angular - Angular - 在组件之间共享数据,获取空数据
问题描述
我正在尝试将从一个组件中的服务接收到的数据共享到另一个组件但获取空数据。
服务:
@Injectable()
export class AService {
private data = new BehaviorSubject<any>(null);
public data$ = this.data.asObservable();
getData(){
// http get service
}
setData(newData) {
this.data.next(newData);
}
}
组件 1:
@Component({
selector: "component1",
templateUrl: "../html/component1.html",
styleUrls: ["../css/component1.css"],
providers: [AService]
})
export class Component1 implements OnInit {
constructor(private aService: AService) {}
getData(corpId: string, type: string) {
this.aService
.getData
.subscribe(newData => {
this.aService.setData(newData);
// some other code.
});
}
}
组件 2:
@Component({
selector: "component2",
templateUrl: "../html/component2.html",
styleUrls: ["../css/component2.css"],
providers: [AService]
})
export class Component2 implements OnInit {
newData: any;
constructor(private aService: AService) {
this.aService.data$.subscribe(data => this.newData = data); // data is null as this statement is getting called first before the component 1
}
}
组件 2 构造函数在组件 1 方法之前被调用getData()
。两个组件都在同一个页面中,我希望先加载组件 1,然后再加载组件,以便将组件 1 中的数据获取到组件 2。
在另一个基本组件中,我使用了两个选择器,如下所示:
<component1></component1>
<component2></component2>
解决方案
我应该建议您将 ngIf (在 html 父级中)和您的服务中的另一个变量结合起来。像这样,当组件 1 完成时,它会更新变量,组件 2 将被加载。
服务:
import { Injectable } from "@angular/core";
import { BehaviorSubject } from "rxjs";
@Injectable()
@Injectable({
providedIn: "root"
})
/**
* Service to manage all the global variables in order to use it in different components
*/
export class AService {
private data = new BehaviorSubject<any>(null);
public data$ = this.data.asObservable();
// Global variable to know if component 1 is done
private isComponent1Source = new BehaviorSubject(false); // Set up the source
isComponent1 = this.isComponent1Source.asObservable(); // Make it Observable
constructor() {}
/**
* Function to change the variable from a component
* @param done boolean to display the info experiment
*/
setIsComponent1(done: any) {
this.isComponent1Source.next(done);
}
getData(){
// http get service
}
setData(newData) {
this.data.next(newData);
}
}
组件 1:
@Component({
selector: "component1",
templateUrl: "../html/component1.html",
styleUrls: ["../css/component1.css"],
providers: [AService]
})
export class Component1 implements OnInit {
constructor(private aService: AService) {}
getData(corpId: string, type: string) {
this.aService
.getData
.subscribe(newData => {
this.aService.setData(newData);
this.aService.setIsComponent1(true);
// some other code.
});
}
}
组件 2
@Component({
selector: "component2",
templateUrl: "../html/component2.html",
styleUrls: ["../css/component2.css"],
providers: [AService]
})
export class Component2 implements OnInit {
newData: any;
constructor(private aService: AService) {
this.aService.data$.subscribe(data => this.newData = data);
}
}
父组件:
@Component({
selector: "componentParent",
templateUrl: "../html/componentparent.html",
styleUrls: ["../css/componentParent.css"],
providers: [AService]
})
export class ComponentParent implements OnInit {
done = false;
constructor(private aService: AService) {
this.aService.isComponent1.subscribe(res => this.done = res);
}
}
父 HTML:
<component1></component1>
<component2 *ngIf="done"></component2>
推荐阅读
- react-native - 我的项目在执行“react-native log-android”时遇到问题
- sqlite - 可以像临时“表”一样分配查询结果吗?
- python - 基于文本的 RPG 重复伤害完成
- google-apps-script - 有没有比使用谷歌应用脚本的 getValues() 函数更快地从谷歌电子表格获取值的方法?
- javascript - 将错误数据从 Firebase 或服务器响应传递给 Formik
- angular - 获取并设置formControl,失控
- d3.js - 圆“打桩”的D3方法(大致是金字塔的形状)
- python - 在对 Oracle SQL 的 Python 调用中设置 echo ON 不起作用
- c# - 如何在 c# 中使用 wkhtmltopdf 为 pdf 添加水印?
- mysql - 如何进行利用 MySQL 后期行查找的 ORM 查询?