使用Angular中的服务方法传递组件之间的数据
我正在使用角度服务在组件之间传递数据。我希望此服务保存 4 个 ID uniId、schoolId、classId 和 StudentId。有 4 个组件:组件 1 设置 uniId,组件 2 使用 uniId 设置 schoolId,组件 3 使用 uniId 和 schoolId 设置 classId,组件 4 使用 uniId、schoolId 和 classId 设置 StudentId。 我想实现以下目标:
- 将所有 4 个 id 设置到服务中。
- 使用service,获取组件视图上的ids,并以Component1 Path -> 的形式用于导航组件2路径->组件3路径-> Component4 使用锚标记的路径(这样当我在第 3 页上时,我可以使用服务中的 id 导航到第 2 页。)
模型类
export interface GlobalData {
uniId: number;
schoolId: number;
classId: number;
studentId: number;
}
服务类
import { Injectable } from '@angular/core';
import { GlobalData } from 'src/models/GlobalData';
import { BehaviorSubject, Observable, Subject } from 'rxjs';
@Injectable({
providedIn: 'root'
})
export class GlobaldataService {
private globalDataSubject = new Subject<GlobalData>();
constructor() { }
public getGlobalData(): Observable<GlobalData> {
return this.globalDataSubject.asObservable();
}
public setGlobalData(globalDataObj: GlobalData) {
return this.globalDataSubject.next(globalDataObj);
}
}
组件 3
globalDataObject: GlobalData;
uniId: number = 0;
schoolId: number = 0;
constructor(private router: Router, private route: ActivatedRoute, private globalDataService: GlobaldataService) {
}
ngOnInit() {
this.route.queryParams.subscribe(params => {
this.uniId = params['uniId'];
this.schoolId = params['schoolId'];
})
this.globalDataService.setGlobalData({
uniId: this.uniId,
schoolId: this.schoolId,
classId: 0,
studentId: 0
});
this.globalDataService.getGlobalData().subscribe(x =>
this.globalDataObject = x)
}
组件 3 视图
<li class="dds__breadcrumb__item" [routerLinkActive]="['link-active']">
<a [routerLink]="['/unis']">Universities</a>
</li>
<li class="dds__breadcrumb__item" [routerLinkActive]="['link-active']">
<a [routerLink]="['/schools']" [queryparams]="{uniId: this.uniId}">Schools</a>
</li>
<li class="dds__breadcrumb__item" [routerLinkActive]="['link-active']">
<a [routerLink]="['/classes']">Classes</a>
</li>
在组件 3 中,当我单击第一个链接时,它将显示所有大学,当我单击第二个链接时,它将显示该大学中已选择 id 的所有学校,并且班级将 npt 导航到任何内容,因为组件 3 是班级。
我没有在服务中获取数据,并且导航不会连接 id。 任何人都可以帮忙解决这个问题吗?
I am using a angular service to pass the data between components. I would like this service to hold the 4 ids uniId, schoolId, classId and studentId. There are 4 components: Component 1 setting uniId, Component2 setting schoolId using uniId, Component 3 setting classId using uniId and schoolId and Component 4 setting studentId using uniId, schoolId and classId.
I want to achieve following:
- Setting all the 4 ids into the service.
- Using service, get the ids on the view of the component and use it for navigation in the form of Component1 Path -> Component2 Path -> Component3 Path -> Component4 Path using the anchor tag (so that when I am on page 3 I can navigate to page 2 using the id from service.)
Model class
export interface GlobalData {
uniId: number;
schoolId: number;
classId: number;
studentId: number;
}
Service Class
import { Injectable } from '@angular/core';
import { GlobalData } from 'src/models/GlobalData';
import { BehaviorSubject, Observable, Subject } from 'rxjs';
@Injectable({
providedIn: 'root'
})
export class GlobaldataService {
private globalDataSubject = new Subject<GlobalData>();
constructor() { }
public getGlobalData(): Observable<GlobalData> {
return this.globalDataSubject.asObservable();
}
public setGlobalData(globalDataObj: GlobalData) {
return this.globalDataSubject.next(globalDataObj);
}
}
Component 3
globalDataObject: GlobalData;
uniId: number = 0;
schoolId: number = 0;
constructor(private router: Router, private route: ActivatedRoute, private globalDataService: GlobaldataService) {
}
ngOnInit() {
this.route.queryParams.subscribe(params => {
this.uniId = params['uniId'];
this.schoolId = params['schoolId'];
})
this.globalDataService.setGlobalData({
uniId: this.uniId,
schoolId: this.schoolId,
classId: 0,
studentId: 0
});
this.globalDataService.getGlobalData().subscribe(x =>
this.globalDataObject = x)
}
Component 3 View
<li class="dds__breadcrumb__item" [routerLinkActive]="['link-active']">
<a [routerLink]="['/unis']">Universities</a>
</li>
<li class="dds__breadcrumb__item" [routerLinkActive]="['link-active']">
<a [routerLink]="['/schools']" [queryparams]="{uniId: this.uniId}">Schools</a>
</li>
<li class="dds__breadcrumb__item" [routerLinkActive]="['link-active']">
<a [routerLink]="['/classes']">Classes</a>
</li>
In Component 3, when I click on first link it would show all unis, when I click on the second link it would show all schools in the uni whose id is selected and classes would npt navigate to anything as Component 3 is Classes.
I am not getting data in service and the navigation does not concatenate ids.
Can anyone please help on this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
三个问题:
您在参数订阅之外设置全局数据。
除非您使用
async
管道,否则可观察订阅不会触发更改检测。因此,您需要使用ChangeDetectorRef
强制执行。queryParams
有一个大写的 P这都可以更简单地重写,如果您只是设置和获取值,则不需要可观察量。
服务
组件 3
尽管我认为当您可以注入服务时通过模板传递
queryParams
没有任何意义。Three problems:
You're setting your global data outside of your params subscription.
Observable subscriptions don't trigger change detection unless you're using the
async
pipe. So you need to force it withChangeDetectorRef
.queryParams
has a capital PThis can all be rewritten much simpler, there's no need for observables if you're simply setting and getting values.
Service
Component 3
Although I don't see a point in passing
queryParams
through the template when you can just inject the service.