我已经创建了一个简单的可注入组件,用于模拟测试,直到我想实现我的提供者来检索用户数据为止。它看起来是这样的:
import { Injectable } from '@angular/core';
@Injectable()
export class Identity {
public userInfo: {id: 1};
}我在我的a组件中请求它,如下所示:
export class TabsPage {
constructor(public identity: Identity) {
console.log(this.identity.userInfo);
}
}身份被注入的很好,但是userInfo没有定义,这是怎么回事?我如何通过我的提供者传递数据?
发布于 2017-07-29 08:43:28
您正在为用户信息定义一个类型(typescript类型),而不是一个值,因此它是undefined。public userInfo = { id: 1 };初始化它的值。(请注意,问题中有一个冒号:,而不是=。
https://stackoverflow.com/questions/45384131
复制相似问题