2016-12-13 10 views
0

私はプロバイダにいくつかの問題があります。コンポーネントで新しいカスタムプロバイダをインポートしようとしましたが、機能しません。 この第二のプロバイダは私が作ったとうまく動作最初のものに基づいています...無効なプロバイダ

これは私のプロバイダである:

import { Injectable} from "@angular/core"; 
import { Router, Routes } from '@angular/router'; 

import ... // All components needed 

@Injectable() 
export class RoutesHelper { 

    private userRoutes: Routes = [ 
     { path: '' , component: HeaderComponent, outlet: 'header' }, 
     ... 
    ]; 


    constructor(
    private router:Router 
    ) {} 

    public load() { 
     this.router.resetConfig(this.userRoutes); 
    } 
} 

そして、これが私の「QuestionComponent」

import { Component, OnInit } from '@angular/core'; 
import { RoutesHelper } from '../_utils/routes.helper'; 

@Component({ 
    selector: 'questions-list', 
    templateUrl: './app/question/questions.component.html', 
    providers: [RoutesHelper] 
}) 

export class QuestionsComponent implements OnInit { 

    constructor(private routes:RoutesHelper) {} 

    ngOnInit() { 
    this.routes.load(); 
    } 
} 

ですが私はこのエラーがあります: "QuestionsComponent"のプロバイダが無効です - プロバイダとタイプのインスタンスのみが許可されています:[?undefined?]

なぜ私は "未定義"のオブジェクトを持っていないのか分かりません。このエラーもありません。

ありがとうございました。

+0

あなたはプロバイダをapp.module.tsファイルに含めましたか? –

+0

はい、プロバイダーはアプリに含まれています; module.ts – Maxime

+0

プロバイダーリストにも同様にリンクをインポートしていますか? –

答えて

0

プロバイダのリンクをapp.module.tsファイルにインポートします。

import {RoutesHelper} from 'your path';

app.module.tsプロバイダリストにプロバイダクラス名を含めます。

プロバイダ:[RoutesHelper、//あなたの他のプロバイダ...]、ただそれだけですRoutesHelperリンクをインポートあなたのQuestionComponentで

。プロバイダを定義する必要はありません。

+0

私は試してみましたが、新しいエラー:「QuestionsComponentのすべてのパラメータを解決できません:(?)」が表示されます。何か案が? – Maxime

0

問題解決済み。

コンポーネントでインポートされたプロバイダのインポートコンポーネントによる最後のエラー(再帰インポート)を回避します。 は、私はこれを使用:userRoutesは私がオーバーライドしたいし、それが動作するだけコンセントが含まれてい

var userRoutes: Routes = [ 
    { path: '' , component: LeftComponent, outlet: 'left' }, 
    { path: '' , component: EmptyComponent, outlet: 'footer' }, 
    { path: '' , component: HeaderComponent, outlet: 'header' } 
]; 

var all = userRoutes.concat(this.router.config); 
this.router.resetConfig(all);