2016-06-11 3 views
2

ディレクティブにシングルトンサービスを注入する際に問題があります。 私はサービスを持っています:Angular2 - シングルトンサービスを指令に挿入する

@Injectable() 
export class AuthService{ ... } 

私はそれをブートストラップに入れました。私はエラーが表示さ

@Directive({ 
    selector: '[protected]' 
}) 
export class ProtectedDirective { 
    constructor(private authService:AuthService) { ... } 
} 

...とコンポーネントコンソールで

@Component({ 
    selector: 'dashboard', 
    directives: [ProtectedDirective], 
    template: '<div protected></div', 
}) 
export class DashboardCmp { } 

の1に追加:

ORIGINAL EXCEPTION: No provider for AuthService! 

bootstrap(AppComponent, [AuthService, ...]); 

私は私のコンポーネントを保護ディレクティブを、作られました

Dashboにプロバイダを追加するとardCmp、すべて正常に動作しますが、シングルトンサービスではありません。私は他のコンポーネントにそのプロパティを設定し、私が指示を出しているときは表示されません。

答えて

1

私は私の問題を解決しました。すべてが大丈夫だったけど

import {AuthService} from '../services/auth.service'; (in protected.directive.ts) 

import {AuthService} from '../Services/auth.service'; (in main.ts) 

はい、それは愚かだと等しくありませんが、それは依存性の注入が不可能になりました。