Angular 2 observable 没有 'map' 建模
Angular 2 observable doesn't 'map' to model
在我学习 Angular 2 的过程中,我使用一个可观察对象通过 API 获取一些数据。像这样:
getPosts() {
return this.http.get(this._postsUrl)
.map(res => <Post[]>res.json())
.catch(this.handleError);
}
我的 post 模型看起来是这样的:
export class Post {
constructor(
public title: string,
public content: string,
public img: string = 'test') {
}
我面临的问题是地图运算符对 Post 模型没有任何作用。例如,我尝试为 img 值设置默认值,但在视图中 post.img 不显示任何内容。我什至用其他模型 (Message[]) 更改了 Post[] 并且行为没有改变。有人可以解释这种行为吗?
当我想在模板中使用计算 属性 时,我遇到了类似的问题。
我在这篇文章中找到了一个很好的解决方案:
您在模型上创建了一个静态方法,该方法接受一个对象数组,然后从映射函数中调用该方法。在静态方法中,您可以调用已经定义的构造函数或使用复制构造函数:
映射方法
getPosts() {
return this.http.get(this._postsUrl)
.map(res => Post.fromJSONArray(res.json()))
.catch(this.handleError);
}
现有构造函数
export class Post {
// Existing constructor.
constructor(public title:string, public content:string, public img:string = 'test') {}
// New static method.
static fromJSONArray(array: Array<Object>): Post[] {
return array.map(obj => new Post(obj['title'], obj['content'], obj['img']));
}
}
复制构造函数
export class Post {
title:string;
content:string;
img:string;
// Copy constructor.
constructor(obj: Object) {
this.title = obj['title'];
this.content = obj['content'];
this.img = obj['img'] || 'test';
}
// New static method.
static fromJSONArray(array: Array<Object>): Post[] {
return array.map(obj => new Post(obj);
}
}
如果您使用的是支持代码完成的编辑器,您可以将 obj
和 array
参数的类型更改为 Post
:
export class Post {
title:string;
content:string;
img:string;
// Copy constructor.
constructor(obj: Post) {
this.title = obj.title;
this.content = obj.content;
this.img = obj.img || 'test';
}
// New static method.
static fromJSONArray(array: Array<Post>): Post[] {
return array.map(obj => new Post(obj);
}
}
您可以使用 as
关键字将 JSON 反序列化为您的对象。
Angular2 文档 a tutorial 可以引导您完成此操作。不过总之...
型号:
export class Hero {
id: number;
name: string;
}
服务:
...
import { Hero } from './hero';
...
get(): Observable<Hero> {
return this.http
.get('/myhero.json')
.map((r: Response) => r.json() as Hero);
}
组件:
get(id: string) {
this.myService.get()
.subscribe(
hero => {
console.log(hero);
},
error => console.log(error)
);
}
在我学习 Angular 2 的过程中,我使用一个可观察对象通过 API 获取一些数据。像这样:
getPosts() {
return this.http.get(this._postsUrl)
.map(res => <Post[]>res.json())
.catch(this.handleError);
}
我的 post 模型看起来是这样的:
export class Post {
constructor(
public title: string,
public content: string,
public img: string = 'test') {
}
我面临的问题是地图运算符对 Post 模型没有任何作用。例如,我尝试为 img 值设置默认值,但在视图中 post.img 不显示任何内容。我什至用其他模型 (Message[]) 更改了 Post[] 并且行为没有改变。有人可以解释这种行为吗?
当我想在模板中使用计算 属性 时,我遇到了类似的问题。
我在这篇文章中找到了一个很好的解决方案:
您在模型上创建了一个静态方法,该方法接受一个对象数组,然后从映射函数中调用该方法。在静态方法中,您可以调用已经定义的构造函数或使用复制构造函数:
映射方法
getPosts() {
return this.http.get(this._postsUrl)
.map(res => Post.fromJSONArray(res.json()))
.catch(this.handleError);
}
现有构造函数
export class Post {
// Existing constructor.
constructor(public title:string, public content:string, public img:string = 'test') {}
// New static method.
static fromJSONArray(array: Array<Object>): Post[] {
return array.map(obj => new Post(obj['title'], obj['content'], obj['img']));
}
}
复制构造函数
export class Post {
title:string;
content:string;
img:string;
// Copy constructor.
constructor(obj: Object) {
this.title = obj['title'];
this.content = obj['content'];
this.img = obj['img'] || 'test';
}
// New static method.
static fromJSONArray(array: Array<Object>): Post[] {
return array.map(obj => new Post(obj);
}
}
如果您使用的是支持代码完成的编辑器,您可以将 obj
和 array
参数的类型更改为 Post
:
export class Post {
title:string;
content:string;
img:string;
// Copy constructor.
constructor(obj: Post) {
this.title = obj.title;
this.content = obj.content;
this.img = obj.img || 'test';
}
// New static method.
static fromJSONArray(array: Array<Post>): Post[] {
return array.map(obj => new Post(obj);
}
}
您可以使用 as
关键字将 JSON 反序列化为您的对象。
Angular2 文档 a tutorial 可以引导您完成此操作。不过总之...
型号:
export class Hero {
id: number;
name: string;
}
服务:
...
import { Hero } from './hero';
...
get(): Observable<Hero> {
return this.http
.get('/myhero.json')
.map((r: Response) => r.json() as Hero);
}
组件:
get(id: string) {
this.myService.get()
.subscribe(
hero => {
console.log(hero);
},
error => console.log(error)
);
}