繁体   English   中英

从服务订阅数据时,类型 void 上不存在错误属性订阅?

[英]error property subscribe doesn't exist on type void when subscribe data from service?

我在 angular 7 应用程序上工作我遇到错误属性订阅不存在类型

从服务订阅数据时无效。

app.component.ts 上的订阅数据 function 上显示错误所以如果可能的话如何解决这个问题?

allReportCategories:any[];
ngOnInit() {


       this._displayreport.getallReportCategories().subscribe((data: any[]) => {  
        this.allReportCategories = data;  

      }); 
}

展示报告服务 ts

allReportCategories:any[];


getallReportCategories(){

    return 

     this.allReportCategories=[
    {
        "reportCategoryID": 1,
        "reportCategory": "Dashboard Parametric",
        "isDeleted": false,
        "menuIcon": "icon-home"
    },
    {
        "reportCategoryID": 2,
        "reportCategory": "Monitor Reports",
        "isDeleted": false,
        "menuIcon": "icon-list"
    },
    {
        "reportCategoryID": 3,
        "reportCategory": "Other Reports",
        "isDeleted": false,
        "menuIcon": "icon-docs"
    },
    {
        "reportCategoryID": 4,
        "reportCategory": "PCN Flow",
        "isDeleted": false,
        "menuIcon": "icon-list"
    },
    {
        "reportCategoryID": 5,
        "reportCategory": "Compliance By Document",
        "isDeleted": false,
        "menuIcon": "icon-home"
    }
];

  }

示例存在于 stackbliz 上,如下所示:

https://stackblitz.com/edit/create-1arrvm?file=app%2Fdisplayreport.service.ts

更具体地说明我的问题

我在这里改变了什么:

this._displayreport.getallReportCategories().subscribe

你需要创建 observable 来订阅

例子:-

在显示报告服务 ts

import { BehaviorSubject } from 'rxjs';


getallReportCategories(){

const allReportCategories = [
    {
        "reportCategoryID": 1,
        "reportCategory": "Dashboard Parametric",
        "isDeleted": false,
        "menuIcon": "icon-home"
    },
    {
        "reportCategoryID": 2,
        "reportCategory": "Monitor Reports",
        "isDeleted": false,
        "menuIcon": "icon-list"
    },
    {
        "reportCategoryID": 3,
        "reportCategory": "Other Reports",
        "isDeleted": false,
        "menuIcon": "icon-docs"
    },
    {
        "reportCategoryID": 4,
        "reportCategory": "PCN Flow",
        "isDeleted": false,
        "menuIcon": "icon-list"
    },
    {
        "reportCategoryID": 5,
        "reportCategory": "Compliance By Document",
        "isDeleted": false,
        "menuIcon": "icon-home"
    }
];


    return new BehaviorSubject(allReportCategories)

  }

在组件中

allReportCategories:any[];
ngOnInit() {


       this._displayreport.getallReportCategories().subscribe((data: any[]) => {  
        this.allReportCategories = data;  

      }); 
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM