angular - 使用 BehaviorSubject Angular 5 在两个组件之间共享对象的麻烦
问题描述
我正在尝试通过服务在两个组件之间共享一个对象。
该组件option-post.component
旨在显示此对象或这些对象的数组。该组件option-post-choix.component
用于选择要option-post.component
显示的对象。我用来option-data.service
在两个组件之间共享数据。
我可以看到我console.log('service recv', data)
的option-data.service
数据到达服务但option-post.component
我可以看到console.log('recv',this.option_select)
我只收到一个空数组。我也使用rxjs/BehaviorSubject
因为option-post
仅在服务发出数据后订阅。
这个逻辑真的受到这个stackoverflow问题和这个答案的启发,但我现在有点无能为力。我已经检查过我的服务是否只提供一次module.ts
我的问题是:为什么数组在option-post.component
通过我的服务后是空的,而他不在?
选项-data.service.ts
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs/Observable';
import {BehaviorSubject} from 'rxjs/BehaviorSubject';
import { Options } from './option-post-choix/option';
@Injectable()
export class OptionDataService {
private getDataSubject: BehaviorSubject<Options[]> = new BehaviorSubject([]);
getData$: Observable<Options[]> = this.getDataSubject.asObservable();
constructor() {
}
getData(data) {
console.log('service recv', data);
this.getDataSubject.next(data);
}
}
option-post-choix.component.ts
import { Options } from './option';
import { OptionDataService } from '../option-data.service';
@Component({
selector: 'app-option-post-choix',
templateUrl: './option-post-choix.component.html',
styleUrls: ['./option-post-choix.component.css']
})
export class OptionPostChoixComponent implements OnInit {
// simulation data
options_choix = [
new Options(0, 'Option 1', 'texte option 1', 15),
new Options(1, 'Option 2', 'texte option 2', 150),
new Options(2, 'Option 3', 'texte option 3', 50)
];
constructor(private location: Location) { }
option_select: Array<Options> = [];
private dataTrans: OptionDataService = new OptionDataService;
onValidClicked() {
this.dataTrans.getData(this.option_select);
this.location.back();
}
addOpt(opt, check) {
console.log('id ', opt.id, 'is', check);
if (!check) {
this.option_select.push(opt);
} else {
this.option_select.splice(this.option_select.findIndex(x => x.id === opt.id), 1);
}
}
选项-post.component.ts
import { OptionDataService } from '../option-data.service';
import { Options } from '../option-post-choix/option';
import { Observable } from 'rxjs/Observable';
@Component({
selector: 'app-option-post',
templateUrl: './option-post.component.html',
styleUrls: ['./option-post.component.css']
})
export class OptionPostComponent implements OnInit {
public option_select: Options[];
constructor(private router: Router, private data: OptionDataService) {
}
ngOnInit() {
this.data.getData$.subscribe(option_select =>
this.option_select = option_select);
console.log('recv', this.option_select); //empty array []
}
addOption() {
this.router.navigate(['/home/optionPostChoix']);
}
}
option-post.component.html
<div class="element_liste">
<div class="option_box" *ngFor="let opt of options_select">
{{opt.titre}}
<div class="option_elem">
{{opt.texte}}
</div>
</div>
</div>
option.ts
是我声明Options
要共享的对象的地方
export class Options {
constructor(
public id: number,
public titre: string,
public texte: string,
public prix: number
) {}
}
解决方案
感谢Nabin Kumar Khatiwada我的问题得到了解决:
option-post-choix.component
从改变
constructor(private location: Location){}
到
constructor(private location: Location, private dataTrans: OptionDataService) {}
并删除
private dataTrans: OptionDataService = new OptionDataService;
推荐阅读
- wso2 - 如何删除 WSO2 IS 中特定 ServiceProvider 的特定用户的会话?
- php - 如何重定向带有特殊字符%ef%bb%bf的URL?
- r - 使用 R:如何按季度日期对案例进行细分,并通过每个季度内的单独变量识别独特案例?
- elasticsearch - 为什么类似的 mget 查询一个索引比另一个慢?
- python - 如何在 Python 中使用 Instagram API 将图像上传到我自己的帐户
- java - 对于删除任何索引处的节点,我无法理解最后一行代码“temp.next = t;”
- artificial-intelligence - 如何通过回溯搜索找到所有可能的解决方案?
- c# - 为 Soap 请求生成用户名令牌
- javascript - 在 Javascript 中,小数位只有双零的数字在转换为 String 时丢失了双零?为什么?
- c# - DelegateCommand async await vs Task.Run UI 锁定