如何在构造函数 1 中访问 constructor2 对象的对象值
How to access object value of constructor2 object in constructor 1
基本上我正在创建一个视频流应用程序并尝试在我的页面中显示 Result 构造函数的名称对象值,但显然它什么也没显示
而且我不知道我哪里出错了。
我在 getVideo() 函数中遇到错误
export class Result {
constructor(public id: string, public name: string, public imageUrl: string) {}
}
import {Result} from './res.model';
export class Video {
constructor(
public categoryId: string,
public category: string,
public results: Result[]
) {}
}
video.service.ts
private _data = new BehaviorSubject<Video[]>(
[
new Video('1', 'Action',
[
new Result('10', 'abc0', 'https://cleantechnica.com/files/2017/10/rimac.jpg'),
new Result('11', 'abc1', 'https://cleantechnica.com/files/2017/10/rimac.jpg'),
new Result('12', 'abc2', 'https://cleantechnica.com/files/2017/10/rimac.jpg')
]),
new Video('2', 'Drama',
[
new Result('20', 'abc3', 'https://cleantechnica.com/files/2017/10/rimac.jpg'),
new Result('21', 'abc4', 'https://cleantechnica.com/files/2017/10/rimac.jpg'),
new Result('22', 'abc5', 'https://cleantechnica.com/files/2017/10/rimac.jpg')
])
]);
get data() {
return this._data.asObservable();
}
getVideo(resultId: string) {
this._data.subscribe(videos => {
return this.videos.find(video => {
return video.results.find(result => { result.id = resultId; });
});
});
}
这是我的detailspage.ts
items: Video;
private videoSub: Subscription;
ngOnInit() {
this.route.paramMap.subscribe(paramMap => {
if (!paramMap.has('results.id')) {
return;
}
this.videoSub = this.videoService.getVideo(paramMap.get('results.id')).subscribe(video => {
this.items = video;
});
});
}
detailspage.html
<ion-card>
<ion-card-content>
{{items.name}}
</ion-card-content>
</ion-card>
getVideo() 使用 map()
getVideos(id: string) {
return this.data.pipe(take(1), map(result => {
return {...result.find(p => {
return {...p.find(videoId => videoId.id === id )};
})};
}));
}
我正在尝试显示 'name' 对象值 abc0、abc1、abc2...
编辑:
您的代码中有错字:
getVideo(resultId: string) {
this._data.subscribe(videos => {
return this.videos.find(video => {
return video.results.find(result => { result.id = resultId; }); // you are using ‘=‘ instead of ‘==‘ here
});
});
问题在这里
getVideo(resultId: string) {
this._data.subscribe(videos => {
return this.videos.find(video => {
return video.results.find(result => result.id === resultId);
});
});
你正在订阅,returns 什么也不会去,你应该使用地图运算符,而且你不需要查找结果你需要检查视频是否有任何具有相同 resultIdd 的结果所以使用一些函数而不是查找
getVideo(resultId: string) {
return this._data.pipe(map(videos => {
return this.videos.find(video => {
return video.results.some(result => result.id===resultId);
});
}));
我想答案差不多就在这里了
videos: Video[];
get data() {
return this._data.asObservable();
}
getVideo(resultId: string) {
this._data.subscribe(videos => {
const _video = videos.filter(video => video.results.map(x => x.id).indexOf(resultId) !== -1);
this.videos = _video;
});
return this.videos;
}
// another way, the same file does not need to subscribe to this file variable
getVideo(resultId: string): Video[] {
const _data = this._data.getValue();
const _videos = videos.filter(video => video.results.map(x => x.id).indexOf(resultId) !== -1);
return _videos;
}
代码可以工作。
基本上我正在创建一个视频流应用程序并尝试在我的页面中显示 Result 构造函数的名称对象值,但显然它什么也没显示 而且我不知道我哪里出错了。
我在 getVideo() 函数中遇到错误
export class Result {
constructor(public id: string, public name: string, public imageUrl: string) {}
}
import {Result} from './res.model';
export class Video {
constructor(
public categoryId: string,
public category: string,
public results: Result[]
) {}
}
video.service.ts
private _data = new BehaviorSubject<Video[]>(
[
new Video('1', 'Action',
[
new Result('10', 'abc0', 'https://cleantechnica.com/files/2017/10/rimac.jpg'),
new Result('11', 'abc1', 'https://cleantechnica.com/files/2017/10/rimac.jpg'),
new Result('12', 'abc2', 'https://cleantechnica.com/files/2017/10/rimac.jpg')
]),
new Video('2', 'Drama',
[
new Result('20', 'abc3', 'https://cleantechnica.com/files/2017/10/rimac.jpg'),
new Result('21', 'abc4', 'https://cleantechnica.com/files/2017/10/rimac.jpg'),
new Result('22', 'abc5', 'https://cleantechnica.com/files/2017/10/rimac.jpg')
])
]);
get data() {
return this._data.asObservable();
}
getVideo(resultId: string) {
this._data.subscribe(videos => {
return this.videos.find(video => {
return video.results.find(result => { result.id = resultId; });
});
});
}
这是我的detailspage.ts
items: Video;
private videoSub: Subscription;
ngOnInit() {
this.route.paramMap.subscribe(paramMap => {
if (!paramMap.has('results.id')) {
return;
}
this.videoSub = this.videoService.getVideo(paramMap.get('results.id')).subscribe(video => {
this.items = video;
});
});
}
detailspage.html
<ion-card>
<ion-card-content>
{{items.name}}
</ion-card-content>
</ion-card>
getVideo() 使用 map()
getVideos(id: string) {
return this.data.pipe(take(1), map(result => {
return {...result.find(p => {
return {...p.find(videoId => videoId.id === id )};
})};
}));
}
我正在尝试显示 'name' 对象值 abc0、abc1、abc2...
编辑:
您的代码中有错字:
getVideo(resultId: string) {
this._data.subscribe(videos => {
return this.videos.find(video => {
return video.results.find(result => { result.id = resultId; }); // you are using ‘=‘ instead of ‘==‘ here
});
});
问题在这里
getVideo(resultId: string) {
this._data.subscribe(videos => {
return this.videos.find(video => {
return video.results.find(result => result.id === resultId);
});
});
你正在订阅,returns 什么也不会去,你应该使用地图运算符,而且你不需要查找结果你需要检查视频是否有任何具有相同 resultIdd 的结果所以使用一些函数而不是查找
getVideo(resultId: string) {
return this._data.pipe(map(videos => {
return this.videos.find(video => {
return video.results.some(result => result.id===resultId);
});
}));
我想答案差不多就在这里了
videos: Video[];
get data() {
return this._data.asObservable();
}
getVideo(resultId: string) {
this._data.subscribe(videos => {
const _video = videos.filter(video => video.results.map(x => x.id).indexOf(resultId) !== -1);
this.videos = _video;
});
return this.videos;
}
// another way, the same file does not need to subscribe to this file variable
getVideo(resultId: string): Video[] {
const _data = this._data.getValue();
const _videos = videos.filter(video => video.results.map(x => x.id).indexOf(resultId) !== -1);
return _videos;
}
代码可以工作。