需要帮助从Angular 2中的Service

时间:2018-06-02 14:58:14

标签: javascript html angular

将用户所在的最后一页的URL ID传递给我可以在对话框中引用的服务。

issuer.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;
  }


}

具有我想要的网址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.

2 个答案:

答案 0 :(得分:1)

您不需要get方法的参数,因为您已经拥有服务中的值

  getUrlid() {
    return this.currentUrlid;
  }

你可以使用如下的方法检索第二个组件中的值,

this.issuerService.currentUrlid.subscribe((value: string) => {
         this.urlid = value;
}

答案 1 :(得分:1)

你可以看到这个example,它是修改过的列表:

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

  • 使用ReplaySubject(1)返回最后一个urlId;它没有默认值,只返回prev一个值(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;
    });
  }
}