Angular 无法读取属性';isXXXXX';未定义的
我试图在我的一个组件上使用共享服务。当我与应用程序根组件一起使用时,它工作正常。当我试图在另一个模块/仪表板上使用时,我得到一个错误 共享/授权.service.tsAngular 无法读取属性';isXXXXX';未定义的,angular,typescript,angular-services,Angular,Typescript,Angular Services,我试图在我的一个组件上使用共享服务。当我与应用程序根组件一起使用时,它工作正常。当我试图在另一个模块/仪表板上使用时,我得到一个错误 共享/授权.service.ts @Injectable() export class AuthorizationService { isDoingSomething() { return "Some Boolean value"; } } @Component({ selector: 'app-header', templateUrl
@Injectable()
export class AuthorizationService {
isDoingSomething() {
return "Some Boolean value";
}
}
@Component({
selector: 'app-header',
templateUrl: './header.component.html',
styleUrls: ['./header.component.css']
})
export class HeaderComponent {
constructor(private _auth: AuthorizeService,
private _router: Router) { }
}
仪表板/标题/标题.component.ts
@Injectable()
export class AuthorizationService {
isDoingSomething() {
return "Some Boolean value";
}
}
@Component({
selector: 'app-header',
templateUrl: './header.component.html',
styleUrls: ['./header.component.css']
})
export class HeaderComponent {
constructor(private _auth: AuthorizeService,
private _router: Router) { }
}
dashboard/header/header.component.html
<mat-toolbar color="primary" role="heading">
<div fxFlex fxLayout fxLayoutAlign="flex-end">
<ul fxLayout fxLayoutGap="10px" class="navigation-items">
<li routerLinkActive="active" [routerLinkActiveOptions]="{exact: true}">
<a routerLink="/home">Home</a>
</li>
<li routerLinkActive="active">
<a *ngIf="_auth.isDoingSomething()" routerLink="/docall">Rest Call</a>
</li>
</ul>
</div>
</mat-toolbar>
我认为您在应用程序中使用了延迟加载。如果是,您必须创建一个共享模块,并在其提供程序中添加您的服务,然后将其导入到您想要使用的每个模块中
@NgModule({
imports: [],
declarations: [],
exports:[],
providers: [
YourService
]
})
export class SharedModule {
}
为您的服务注入器尝试此操作
@Injectable({providedIn:'root'})