(Angular 4 + firebase + service + component)将变量从服务传递到组件

时间:2017-06-19 12:28:16

标签: angular firebase service components angularfire

我想发送变量" dataJSONparse "从服务到组件

服务

listOfUsers() {
this.items = <FirebaseListObservable<any>>this.af.list('/user', {
    query: {
    orderByChild: 'name'
    }
}).map((user) => {
    return user.map(user_data => {
    user_data.name;
    user_data.mobile;
    return user_data;
    })
})

    this.userDataQuery = this.items.subscribe(x => this.forx(x));
}

public dataJSONparse: any;
forx(x) {
    this.dataStringify = JSON.stringify(x);
    this.dataJSONparse = JSON.parse(this.dataStringify);
    console.log("test print from service: ",this.dataJSONparse);
}

组件

  ngOnInit(): void {
    console.log("test",this.heroservice.dataJSONparse);
  }

控制台错误

  

从组件未定义测试

     

从服务测试打印:数组[对象,对象,对象,对象,对象,对象]

Error print screen

0 个答案:

没有答案