我有一个共享服务,它总是返回undefined。在App.component中,从html页面成功检索到值,但是,欢迎组件会丢失该值。
CSHTML
<pm-app gwLinkID="111"></pm-app>
共享服务
import { Subject } from 'rxjs/Subject';
@Injectable()
export class AppParamasService {
constructor() {
}
private _ntName = new Subject<any>();
ntNameValue = this._ntName.asObservable();
private _gwLinkID = new Subject<any>();
gwLinkIDValue = this._gwLinkID.asObservable();
gwLinkIDReceived(gwLinkID: number) {
this._gwLinkID.next(gwLinkID);
}
}
app.component
import { Component, OnInit, Input, ElementRef } from '@angular/core';
import { AppParamasService } from './shared/app-params.service';
@Component({
selector: 'pm-app',
moduleId: module.id,
template:'<home></home>'
})
export class AppComponent {
@Input() gwLinkID: number;
@Input() psnlUID: string;
@Input() ntName: string;
//pageTitle: any[];
errorMessage: string;
constructor(elm: ElementRef, private _param: AppParamasService) {
this.gwLinkID = elm.nativeElement.getAttribute('gwLinkID'); //value is coming
this._param.gwLinkIDReceived (elm.nativeElement.getAttribute('gwLinkID'));
}
ngOnInit(): void {
}
}
welcome.component this.gwLinkID未定义
import { Component, ElementRef,Input} from '@angular/core';
@Component({
selector: 'home',
moduleId: module.id,
templateUrl: 'welcome.component.html'
})
export class WelcomeComponent {
gwLinkID: number;
constructor(elm: ElementRef,private _appParams: AppParamasService) {
this._appParams.gwLinkIDValue.subscribe(gwLinkIDVal => {
this.gwLinkID = gwLinkIDVal;
});
}
}