【发布时间】:2017-06-08 09:37:15
【问题描述】:
我正在尝试从由 navMenu.component 设置并由 tab.component 使用的共享服务中获取值。我觉得它已正确共享,但到 tab.component 获得价值时为时已晚。我尝试使用 Elvis 运算符,但出现了一个错误。
启动 app.component.html
<div>
<div class="outer">
<nav-menu [gwLinkID]="gwLinkID" [psnlUID]="psnlUID" [ntName]="ntName"></nav-menu>
</div>
<div class="main">
<div class="container">
<tab></tab>
</div>
</div>
共享服务的值由 navMenu.component 设置
@Component({
selector: 'nav-menu',
moduleId: module.id,
templateUrl: 'navMenu.component.html'
})
export class NavMenuComponent implements OnInit {
tabs: INavMenuTabs[];
@Input() gwLinkID: number;
@Input() psnlUID: string;
@Input() ntName: string;
pageTitle: any[];
title: string;
titleID: number;
appParams: IAppParams;
errorMessage: string;
linkData: any;
constructor(private _appParams: AppParamasService, private _navMenuService: NavMenuService) {
// this.gwLinkID = this._appParams.LinkID;
console.log('inside navMenu.component constructor ' + this._appParams.GetLinkName);
}
getTabs() {
this._navMenuService.getTabs(this._appParams.LinkID, 'ciiw')
.subscribe(
data => {
this.tabs = data.result
},
error => this.errorMessage = <any>error),
this.appParams = this._appParams.GetParams();
console.log('inside navMenu.component method getTabs ' + this._appParams.GetLinkName);
}
ngOnInit(): void {
this._navMenuService.getLinkName(this.gwLinkID)
.subscribe(
data => {
this.titleID = data.result.LinkID;
this._appParams.SetLinkID = data.result.LinkID;
this.title = data.result.LinkName;
this._appParams.SetLinkName = this.title;
this._appParams.SetParams(this.psnlUID, this.ntName, this.gwLinkID);
this.getTabs(); // call the tabs-method here, data is available here!
});
console.log('inside navMenu.component method ngOnInit ' + this._appParams.GetLinkName);
}
}
共享服务
@Injectable()
export class AppParamasService {
params: IAppParams;
constructor() {
}
private _linkName: string;
get GetLinkName(): string {
return this._linkName;
}
set SetLinkName(value: string) {
this._linkName = value;
}
}
tab.component 使用共享服务方法
@Component({
selector: 'tab',
moduleId: module.id,
templateUrl: 'tab.component.html'
})
export class TabComponent implements OnInit {
linkName: string;
appParams: IAppParams;
constructor(private _appParams: AppParamasService) {
console.log('inside tab.component constructor ' + this._appParams.GetLinkName);
}
CheckValue()
{
console.log('inside tab.component CheckValue ' + this._appParams.GetLinkName);
}
ngOnInit(): void {
this.linkName = this._appParams.GetLinkName;
console.log('inside tab.component ngOnInt ' + this._appParams.GetLinkName);
}
}
tab.component.html
<div class="team">
<h3 *ngIf="linkName">{{linkName}}</h3>
</div>
如果我将 navMenu.component.ts 中的构造函数更改为
构造函数(私有_appParams:AppParamasService,私有 _navMenuService: NavMenuService) {
this._appParams.SetLinkName = 'Test'; }
一切正常,并且该值可用于 tab.component.ts,因此在哪个阶段值可用于其他组件是一个问题
控制台日志的输出
- navMenu.component 构造函数内部未定义
- tab.component 构造函数内部未定义
- navMenu.component 方法 ngOnInit 内部未定义
- tab.component ngOnInt 内部未定义
- navMenu.component 方法内部 getTabs 财务计划报告 管理员
【问题讨论】:
-
只是一个注释,“?.” 不是猫王算子,它是安全导航算子。
-
您能否尝试将控制台日志添加到
set SetLikName(value: string) {this._linkName = value; console.log(this._linkName)}。它有价值吗? -
@AJT_82,那个博主搞混了。您会在支持它们的每个官方语言文档中找到这两个运算符的正确定义:docs.groovy-lang.org/latest/html/documentation/#_elvis_operator、fantom.org/doc/docLang/Expressions,最后但并非最不重要的是:en.wikipedia.org/wiki/Elvis_operator
-
谢谢大家,但这并不能解决我的问题。我发现一件事编辑了上面的代码。如果我在 navMenu.componenet.ts 的构造函数中硬编码值 this._appParams.SetLinkName = 'Test';它可以工作,并且该值可用于 tab.component.ts。
标签: angular