2017-11-02 10 views
0

私はこの角度ルータ

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

DEMO

のように、ルータに直接いくつかのデータを解決しようとしている、私はこのルートに移動したときに私が取得して、いくつかのJSONを解決する方法エラー

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

私が持っている疑問を、以下、私が解決できる方法である静的json

+0

チェックhttps://angular.io/guide/router#resolve-pre-fetching-component-data – ranakrunal9

+0

これは他の問題の解決法を説明しています。私はちょうど(https://blog.thoughtram.io/angular/2016/10/10/resolving-route-data-in-angular-2.html) –

+0

[ここ]説明、私のルートにいくつかの静的なデータが欲しいです私はあなたがAppModuleであなたのRouterModule rootとして()宣言し、最も重要なを逃したと思うし、管理するために、ルート要求をメインプロバイダ/サービスを定義します。私はすべてやったhttps://angular.io/guide/router#add-heroes-functionality – andrea06590

答えて

1

変更resolvedataへ:

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) { } 
} 
関連する問題