Angular如何在订阅后从服务访问数据

时间:2018-01-11 15:58:54

标签: angular observable angular-services

这是我的服务

getDataDetails(id: any) {
     this.dataDocumment = this.afs.doc('data/' + id);
     return this.data = this.dataDocumment.valueChanges().subscribe(res =>{
       this.data = res;
       console.log(this.data);
     });
   }

这是我希望编写数据的组件

ngOnInit() {
    this.id = this.route.snapshot.params['id'];
    this.dataD = this.dataService.getDataDetails(this.id);
    console.log(this.dataD);
  }

服务的显示数据在控制台中显示良好

{adresse: "Rue de France #10", bureau: "78", centre: "Lycée Français", cin: "751-02-785-782-002", commune: "Paris", …}

但它会从我的组件

显示此消息
Subscriber {closed: false, _parent: null, _parents: null, _subscriptions: Array(1), syncErrorValue: null, …}

PS:dataD是“any”类型

我正确地在服务中进行了订阅,为什么我在我的组件中展示了这个?

1 个答案:

答案 0 :(得分:0)

从服务中返回observable并在组件内部进行订阅。

Each cable consists of several components. 

Harness  WireName  Color
12345    ***0001   BLU
12345    ***0001   WHT
12345    ***0001   ORG
12345    ***0001   GRN

The Cable code (0001) is going to be the same for all conductors within
the same wire. 
For instance If you had two twistedpair cables, it would be:

Harness  WireName  Color
12345    ***0001   BLU
12345    ***0001   WHT
12345    ***0002   BLU
12345    ***0002   WHT
12345    ***0015   BLU
12345    ***0015   WHT