gpt4 book ai didi

javascript - 需要帮助从 Angular 2 中的服务访问变量

转载 作者:行者123 更新时间:2023-12-01 02:00:12 24 4
gpt4 key购买 nike

将用户上次访问的页面中的 URL ID 传递到我可以在对话框中引用的服务。

发行者.service.ts

import { Injectable, EventEmitter } from '@angular/core';
import { Observable, of } from 'rxjs';
import { BehaviorSubject } from 'rxjs';

@Injectable()
export class IssuerService {


private urlidSource = new BehaviorSubject<string>('');
currentUrlid = this.urlidSource.asObservable();
public onChange: EventEmitter<string> = new EventEmitter<string>();

constructor () {
}

changeUrlid(urlid: string) {
this.currentUrlid = of(urlid);
this.onChange.emit(urlid);
}

getUrlid(currentUrlid: string) {
return this.currentUrlid;
}


}

具有我想要的 URL ID 的页面 (dashboard.component.ts)

import { IssuerService } from './../../issuer.service';
import { ActivatedRoute } from '@angular/router';
import { Router } from '@angular/router';
urlid: string;
constructor(
private route: ActivatedRoute,
private router: Router,
private issuerService: IssuerService,
public dialog: MatDialog
) {}

newUrlid() {
this.issuerService.changeUrlid(this.route.snapshot.paramMap.get('id'));
console.log(this.urlid);
}

ngOnInit() {
// Get URL ID
this.issuerService.onChange.subscribe(urlid => this.urlid = urlid);
this.newUrlid();
}

我想读取其中的值的组件:

    import { ActivatedRoute } from '@angular/router';
import { Router } from '@angular/router';
import { IssuerService } from './../../issuer.service';

urlid: string;

constructor(
private route: ActivatedRoute,
private router: Router,
private issuerService: IssuerService,
public dialog: MatDialog
) {}

ngOnInit() {
this.issuerService.onChange.subscribe(urlid => {
this.urlid = urlid;
console.log(this.urlid);
});

}

因此,当前当我访问仪表板页面时,它将显示正确的值 2。我的目标是当用户访问任何页面时我可以读取这个值 2。我如何访问这个值?上面的代码有效,我的标题显示 2,但仅在仪表板页面上时才显示。无论用户在哪个页面,我都需要它显示 2。

最佳答案

你可以看到这个example ,以及修改后的列表:

  • 使用 queryPamas 获取查询字符串,而不是参数(DashboardComponent)

  • 使用ReplaySubject(1)返回最后一个urlId;它没有默认值,只返回前一个值(IssuerService)

  • 从 getUrlid 获取 observable 并在想要显示 url id 的组件中订阅它

enter image description here

export class IssuerService {


private urlidSource = new ReplaySubject<string>(1);

constructor() {
}

changeUrlid(urlid: string) {
this.urlidSource.next(urlid);
}

getUrlid() {
return this.urlidSource;
}

}

export class DashboardComponent implements OnInit {

urlid: string;
constructor(
// private route: ActivatedRoute,
private router: Router,
private issuerService: IssuerService,
// public dialog: MatDialog
) { }

newUrlid() {
// Get URL ID
this.route.queryParams.subscribe((queryParam) => {
const id = queryParam['id'];
if (!id) {
return;
}
this.issuerService.changeUrlid(id);
});
}

ngOnInit() {
this.newUrlid();

this.issuerService.getUrlid().subscribe(urlid => {
this.urlid = urlid;
});
}

}

export class HelloComponent implements OnInit {
urlid;
constructor(
private issuerService: IssuerService
) { }

ngOnInit() {
this.issuerService.getUrlid().subscribe(urlid => {
this.urlid = urlid;
});
}
}

关于javascript - 需要帮助从 Angular 2 中的服务访问变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50658155/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com