2016-06-11 3 views
2

지시문에 싱글 톤 서비스를 주입하는 데 문제가 있습니다. 나는 서비스를 가지고있다 :Angular2 - 싱글 톤 서비스를 지시문에 삽입

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

내가 부트 스트랩퍼에 넣었다.

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

내가 지시했다, 내 구성 요소 보호 :

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

을 ... 그리고

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

내가 오류를 참조 구성 요소 중 하나에 추가 :

ORIGINAL EXCEPTION: No provider for AuthService! 

Dashbo에 제공자를 추가하는 경우 ardCmp, 다 잘 작동하지만 싱글 톤 서비스는 아닙니다. 나는 다른 구성 요소에 속성을 설정하고 지침에있을 때는 표시되지 않습니다.

답변

1

내 문제가 해결되었습니다. 모든 것이 괜찮다고하지만

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

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

네, 바보 같지 않은,하지만 의존성 주입이 불가능했다.

관련 문제