我想将 angular2 observable 转换为 class
I want to convert a angular2 observable to a class
我是 angular2 的新手,我花了很多时间来寻找解决方案,但我没有。我想从 class 中的 http 调用存储转换一个 oberservable。
我有以下内容:
json 文件
[{
"nickname": "magicMike",
"id": "123",
"name": "Michael",
"nachname": "Fischer",
"pictURL": "../images/mainPanel/Image_dummy.jpg",
"city": "Ulm"
}]
用户class文件:
export class User {
nickname: string;
id: string;
name: string;
nachname: string;
pictURL: string;
city: string;
constructor(
nickname: string,
id: string,
name: string,
nachname: string,
pictURL: string,
city: string
){
this.nickname = nickname;
this.id = id;
this.name = name;
this.nachname = nachname;
this.pictURL = pictURL;
this.city = city;
}
}
读取json文件的服务
import { Component, Input } from '@angular/core';
import { Injectable } from '@angular/core';
import { Http, Response, Headers, RequestOptions } from '@angular/http';
import {Observable} from 'rxjs/Rx';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/catch';
import { User } from './user'
@Injectable()
export class UserService {
user: Array<User>;
private useUrl = '/json/user.json';
constructor(private http: Http) {
}
getUser(): Observable<any> {
return this.http.get(this.useUrl)
.map(this.extractData)
.do(data => console.log("User from json: " + JSON.stringify(data)))
.catch(this.handleError);
}
private extractData(response: Response) {
let body = response.json();
return body || {};
}
private handleError(error: Response) {
console.log(error);
return Observable.throw(error.json().error || "500 internal server error");
}
以及获取可观察对象并将其存储到数组的组件
.......
export class AppComponent implements OnInit {
public user: User[];
ngOnInit() {
this.userService.getUser()
.map((user: Array<any>) => {
let result:Array<User> = [];
if (user) {
user.forEach((erg) => {
result.push(new User(erg.nickname, erg.id, erg.name, erg.nachname, erg.pictURL, erg.city ));
});
}
})
.subscribe(user => this.user = user);
}
....
当我 运行 这样做时,我收到以下错误。
C:/Users/Lenovo/Documents/ui-challenger.one/src/app/app.component.ts (53,26): Type 'void' is not assignable to type 'User[]'.)
我希望那里的任何人都可以帮助我。我只想将 json 文件解析为 class,这样我就可以读取这样的属性 "User.name"
ngOnInit() {
this.userService.getUser()
.map((user: Array<any>) => {
let result:Array<User> = [];
if (user) {
user.forEach((erg) => {
result.push(new User(erg.nickname, erg.id, erg.name, erg.nachname, erg.pictURL, erg.city ));
});
}
return result; // <<<=== missing return
})
.subscribe(user => this.user = user);
}
您不需要手动将 json 解析为对象。你做类似下面的事情 -
getUser(): Observable<User[]> {
return this.http.get(this.useUrl)
.map(data => <User[]>data);
}
我是 angular2 的新手,我花了很多时间来寻找解决方案,但我没有。我想从 class 中的 http 调用存储转换一个 oberservable。
我有以下内容:
json 文件
[{
"nickname": "magicMike",
"id": "123",
"name": "Michael",
"nachname": "Fischer",
"pictURL": "../images/mainPanel/Image_dummy.jpg",
"city": "Ulm"
}]
用户class文件:
export class User {
nickname: string;
id: string;
name: string;
nachname: string;
pictURL: string;
city: string;
constructor(
nickname: string,
id: string,
name: string,
nachname: string,
pictURL: string,
city: string
){
this.nickname = nickname;
this.id = id;
this.name = name;
this.nachname = nachname;
this.pictURL = pictURL;
this.city = city;
}
}
读取json文件的服务
import { Component, Input } from '@angular/core';
import { Injectable } from '@angular/core';
import { Http, Response, Headers, RequestOptions } from '@angular/http';
import {Observable} from 'rxjs/Rx';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/catch';
import { User } from './user'
@Injectable()
export class UserService {
user: Array<User>;
private useUrl = '/json/user.json';
constructor(private http: Http) {
}
getUser(): Observable<any> {
return this.http.get(this.useUrl)
.map(this.extractData)
.do(data => console.log("User from json: " + JSON.stringify(data)))
.catch(this.handleError);
}
private extractData(response: Response) {
let body = response.json();
return body || {};
}
private handleError(error: Response) {
console.log(error);
return Observable.throw(error.json().error || "500 internal server error");
}
以及获取可观察对象并将其存储到数组的组件
.......
export class AppComponent implements OnInit {
public user: User[];
ngOnInit() {
this.userService.getUser()
.map((user: Array<any>) => {
let result:Array<User> = [];
if (user) {
user.forEach((erg) => {
result.push(new User(erg.nickname, erg.id, erg.name, erg.nachname, erg.pictURL, erg.city ));
});
}
})
.subscribe(user => this.user = user);
}
....
当我 运行 这样做时,我收到以下错误。
C:/Users/Lenovo/Documents/ui-challenger.one/src/app/app.component.ts (53,26): Type 'void' is not assignable to type 'User[]'.)
我希望那里的任何人都可以帮助我。我只想将 json 文件解析为 class,这样我就可以读取这样的属性 "User.name"
ngOnInit() {
this.userService.getUser()
.map((user: Array<any>) => {
let result:Array<User> = [];
if (user) {
user.forEach((erg) => {
result.push(new User(erg.nickname, erg.id, erg.name, erg.nachname, erg.pictURL, erg.city ));
});
}
return result; // <<<=== missing return
})
.subscribe(user => this.user = user);
}
您不需要手动将 json 解析为对象。你做类似下面的事情 -
getUser(): Observable<User[]> {
return this.http.get(this.useUrl)
.map(data => <User[]>data);
}