2017-04-18 24 views
0

我有我的部分代碼類型爲void是不能分配給輸入任何

export class AddNewCardComponent { 

    public concept = []; 
    constructor( 
     private _router: Router, 
     private _empDiscService: empDiscService) { 

     } 

    ngOnInit(){ 
     this.concept = this._empDiscService.StoreMaster() 
     .subscribe((data)=>{ 
      this.concept = data; 
     }); 
    } 
} 

它只是我想從服務中獲得VAR並把它放在我的店裏= [];

但它說

void類型是不能分配給輸入任何

這裏是我的服務代碼

import {Injectable} from '@angular/core'; 
import { Httpprovider } from './httpprovider'; 

@Injectable() 
export class empDiscService{ 

    public storedata = []; 
    constructor(private _httpprovider: Httpprovider){ 

    } 

    StoreMaster():Observable<any[]>{ 
     return this._httpprovider.httpReq('http://192.168.1.40:5000/getconcept','GET',null,null).map(res =>{<any[]>res.json()} 
    }; 
} 

回答

0

您不得subscribe()裏面的服務,並返回直接對象,這不是RxJs的意圖。

export class empDiscService{ 

    public storedata = []; 
    constructor(private _httpprovider: Httpprovider){ 

    } 
    StoreMaster():Observable<any[]>{ 
     return this._httpprovider.httpReq('http://192.168.1.40:5000/getmapmasterstore','GET',null,null).map(res =>{<any[]>res.json()} 
    } 

} 

你的組件應該是

constructor( 
     private _router: Router, 
     private _empDiscService: empDiscService) { 
      this.store = this._empDiscService.StoreMaster() 
      .subscribe((data)=>{ 
        this.storedata = data; 
     }); 
} 

,你所做的實際的錯誤是服務方法不返回任何東西

StoreMaster():Observable<any[]>{} 

這將解決您的問題

+0

我得到一個錯誤無法讀取屬性未定義在我的組件「訂閱」我需要進口什麼? – Rommy

+0

@ StoreMaster()函數中丟失@Rommy return語句。檢查更新的一個 – Aravind

+0

我嘗試你的,組件上有一些錯誤,當我們提供數據時,現在有新的東西,我得到了這個錯誤>用戶Array.slice是不是一個功能任何想法? – Rommy

0

StoreMaster( )方法不返回任何東西。您可能需要更改服務方法返回觀察到:

StoreMaster(){ 
return this._httpprovider.httpReq('http://192.168.1.40:5000/getmapmasterstore','' + 
    ' GET',null,null).subscribe((data)=>{ 
    var brData = []; 
    for (let i=0;i<data.length;i++){ 
    brData.push(data[i]); 
    } 
    return brData; 
}); 
} 

和ngOnInit

ngOnInit(){ 
     this._empDiscService.StoreMaster() 
      .subscribe(store => this.store = store); 
    } 
+0

,這也會引發相同的錯誤。 – Aravind

+0

我得到一個錯誤無法讀取未定義的屬性「訂閱」,以及我需要導入任何東西? – Rommy

2
import { JobService } from './../services/job.service'; 
import { Component, OnInit } from '@angular/core'; 



@Component({ 
    selector: 'app-job-list', 
    templateUrl: './job-list.component.html', 
    styleUrls: ['./job-list.component.css'] 
}) 
export class JobListComponent implements OnInit { 

**jobs:any = [];** 
erreur = ''; 
    constructor(private jobservice:JobService) { } 

    ngOnInit() { 
    this.jobservice.getJobs().subscribe(

     data => this.jobs = data, 
     error => {console.log ('error')} 

    ); 


    } 

} 
+0

我有同樣的問題,我在我的變量上添加任何類型。 – user2907200

+0

而不是評論你的答案,你可以添加到描述文字的答案,以改善你的答案 – Kalamarico

相關問題