[英]how to display data from function in another component in angular2?
I've got a component dataservice.ts
with a function() that will provide a string by: 我有一个组件
dataservice.ts
和一个function(),它将通过以下方式提供字符串:
httpString: string = "https://";
complUrl: string = "";
something: string = "something";
something: string = ".something";
constructor(private http: Http) {
}
buildUrl(): void {
let pId = "015111810666";
let dId = "i21wcwg2hssv2t9";
this.complUrl = this.httpString + this.something + this.something + pId + dId;
console.log(complUrl);
}
and i want to query complUrl
in another component - myComponent.ts
- by OnInit 而且我想通过OnInit查询另一个组件中的
complUrl
- myComponent.ts
constructor(private myDataService: MyDataService) {
}
ngOnInit() {
this.myDataService.buildUrl();
}
what am i doing wrong? 我究竟做错了什么? i guess it's simple, but i can't figure it out.
我想这很简单,但我不知道。
You are missing this
in your console.log, which causes your error message: complUrl is not defined
, so it should be: 您在console.log中丢失了
this
,这会导致您出现错误消息: complUrl is not defined
,因此应为:
console.log(this.complUrl);
And just as a comment, you have replaced "real" variables with something
, right? 就像评论一样,您用
something
替换了“真实”变量,对吗? Otherwise you have two identical variables. 否则,您有两个相同的变量。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.