typescript 类型 void 不可分配给任何类型
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43462891/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
type void is not assignable to type any
提问by Rommy
I have a code in my component
我的组件中有一个代码
export class AddNewCardComponent {
public concept = [];
constructor(
private _router: Router,
private _empDiscService: empDiscService) {
}
ngOnInit(){
this.concept = this._empDiscService.StoreMaster()
.subscribe((data)=>{
this.concept = data;
});
}
}
its just i want to get var from services and put it in my store = [];
它只是我想从服务中获取 var 并将其放入我的商店 = [];
but it says
但它说
type void is not assignable to type any
类型 void 不可分配给任何类型
here is my code in services
这是我的服务代码
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()}
};
}
采纳答案by Aravind
You shall not subscribe()
inside the service and return the object directly, which is not the intention of RxJs.
您不应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()}
}
}
Your component should be
你的组件应该是
constructor(
private _router: Router,
private _empDiscService: empDiscService) {
this.store = this._empDiscService.StoreMaster()
.subscribe((data)=>{
this.storedata = data;
});
}
The actual mistake which you made is the service method was not returning anything
您犯的实际错误是服务方法没有返回任何内容
StoreMaster():Observable<any[]>{}
This will fix your problem
这将解决您的问题
回答by user2907200
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')}
);
}
}
回答by Julia Passynkova
StoreMaster() method does not return anything. You might need to change the service method to return the observable:
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;
});
}
and ngOnInit
和 ngOnInit
ngOnInit(){
this._empDiscService.StoreMaster()
.subscribe(store => this.store = store);
}