Angular 2 Injectable缺少对象

joh*_*y 5 0 javascript typescript ionic2 angular

我已经创建了一个简单的注射器,我正在使用它进行模拟测试,直到我想要实现我的提供者来检索用户数据.它看起来像这样:

import { Injectable } from '@angular/core';

@Injectable()
export class Identity {
   public userInfo: {id: 1};
}
Run Code Online (Sandbox Code Playgroud)

我在我的aa组件中请求它是这样的:

export class TabsPage {
    constructor(public identity: Identity) {
        console.log(this.identity.userInfo);
    }
}
Run Code Online (Sandbox Code Playgroud)

Identity是Injected正常但userInfo未定义,这里发生了什么?如何通过我的提供商传递数据?

Jua*_*uan 5

您正在为用户信息定义类型(打字稿类型),而不是值,因此它是undefined.public userInfo = { id: 1 };初始化它的价值.(注意你的问题中有一个冒号:,而不是=.