【发布时间】:2018-08-27 04:21:10
【问题描述】:
我正在使用 Angular 5.1.1。我有一个带有导航服务的应用程序,我将它注入到我的组件中。
服务
@Injectable()
export class NavigationService {
...
}
组件:
import { Component } from '@angular/core';
import { NavigationService } from './services/navigation.service';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.less'],
providers: [NavigationService]
})
export class AppComponent {
title = 'App Title';
constructor(private navigationService: NavigationService) { }
}
<div *ngIf="navigationService.navigationModel != null">
<app-navigation-head></app-navigation-head>
<div class="flex-container">
<div class="nav-container">
<app-navigation-side></app-navigation-side>
</div>
<div class="page-container">
<app-data-page></app-data-page>
</div>
</div>
</div>
当我使用 ng serve 运行应用程序时,一切都很好。当我用 ng build 构建它时。注入有效,应用程序运行良好。 但是当我想用 ng build --prod 将它构建为生产时,我得到了错误:
src/app/app.component.html(1,6) 中的错误::属性“navigationService”是私有的,只能在“AppComponent”类中访问。
但是为什么呢?我注射有什么问题吗?
【问题讨论】:
-
如果您在模板文件中使用变量
navigationService,那么如果您使用aot构建,则会出现错误。 -
啊,好吧,如果我将服务更改为公共,它就可以工作。谢谢!
-
在下面写答案)
标签: angular build code-injection