如何从 Singleton 获取变量?

发布于 2025-01-09 10:15:53 字数 1276 浏览 2 评论 0原文

我试图创建一个单例以从不同的组件获取两个变量,这些变量是由始终在其他组件之前执行的组件定义的。

我遇到的问题是 Singleton 实例未保存,当从另一个组件访问它时,它会创建一个新实例,从而丢失它保存的变量。

下面我附上单例类的代码,如何设置变量以及如何尝试从其他组件获取它们:

Singleton.ts

export class Singleton {
  private static instance: Singleton = new Singleton();
  private var1: string = '';
  private var2: string = '';

  private constructor() {}

  public static getInstance() {
    if (!this.instance) {
      this.instance = new Singleton();
    }
    return this.instance;
  }

  public getVar1():string {
    return this.var1;
  }

  public getVar2():string {
    return this.var2;
  }

  public setVar(var1:string, var2:string):void {
    this.var1= var1;
    this.var2 = var2;
  }
}

home.component.ts

ngOnInit() {
    var var1 = "example1"
    var var2 = "example2"
    let global = Singleton.getInstance();
    global.setVar(var1, var2);
    console.log(global);
  }

< home.component.ts

ngOnInit() {
    let global = Singleton.getInstance();
    this.var1 = global.getVar1();
    this.var2 = global.getVar1();
    console.log(global);
  }

中的 console.log() 打印 var1=example1 和 var2=example2,而 call.component.ts 中打印 var1='' 和var2=''。

I am trying to make a singleton to get two variables from different components, these variables are defined by a component that is always executed before the others.

The problem I have is that the Singleton instance is not saved and when accessing it from another component it creates a new one, thus losing the variables that it had saved.

Below I attach the code of the singleton class, how I set the variables and how I try to get them from the other component:

Singleton.ts

export class Singleton {
  private static instance: Singleton = new Singleton();
  private var1: string = '';
  private var2: string = '';

  private constructor() {}

  public static getInstance() {
    if (!this.instance) {
      this.instance = new Singleton();
    }
    return this.instance;
  }

  public getVar1():string {
    return this.var1;
  }

  public getVar2():string {
    return this.var2;
  }

  public setVar(var1:string, var2:string):void {
    this.var1= var1;
    this.var2 = var2;
  }
}

home.component.ts

ngOnInit() {
    var var1 = "example1"
    var var2 = "example2"
    let global = Singleton.getInstance();
    global.setVar(var1, var2);
    console.log(global);
  }

call.component.ts

ngOnInit() {
    let global = Singleton.getInstance();
    this.var1 = global.getVar1();
    this.var2 = global.getVar1();
    console.log(global);
  }

The console.log() from home.component.ts prints var1=example1 and var2=example2, instead in call.component.ts prints var1='' and var2=''.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

风流物 2025-01-16 10:15:54

如果您希望在另一个组件中使用相同的值,那么您需要使用输入和输出装饰器。查看下面的链接

https://angular.io/guide/inputs-outputs

if you want the same value in another component then you need to use input and output decorators . check out below link

https://angular.io/guide/inputs-outputs

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文