我有一个带有function()的组件dataservice.ts
,它将通过以下方式提供字符串:
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);
}
我想查询另一个组件中的complUrl
- myComponent.ts
- OnInit
constructor(private myDataService: MyDataService) {
}
ngOnInit() {
this.myDataService.buildUrl();
}
我在做错了什么?我觉得这很简单,但我无法弄清楚。
答案 0 :(得分:1)
您在console.log中缺少this
,这会导致出现错误消息:complUrl is not defined
,因此应该是:
console.log(this.complUrl);
就像评论一样,你已经取代了#34;真实的"变量something
,对吧?否则你有两个相同的变量。