如何用 angular 路由器解决一些 json

How to resolve some json with the angular router

我正在尝试直接在路由器中解析一些数据,就像这样

export const routes: Routes = [
    {
        path: 'users/:userId',
        component: UserComponent,
        resolve: {
            profile: { name: 'John Doe', .... }
        }
    }
];

DEMO

因此,当我导航到这条路线时,出现以下错误

ERROR Error: Uncaught (in promise): Error: No provider for John Doe!

我的问题是如何解析静态 json

resolve改为data:

export const routes: Routes = [
    {
        path: 'users/:userId',
        component: UserComponent,
        data: {
            profile: { name: 'John Doe', .... }
        }
    }
];

通过路由快照访问该数据:

this.route.snapshot.data['profile'];

其中 this.route 是注入到您正在使用的组件中的激活路由服务。

export class UserComponent {
  constructor(private route: ActivatedRoute) { }
}