angular无法将值设置给公共变量。
创始人
2024-10-30 01:31:15
0

在Angular中,可以使用服务(Service)来共享数据和状态,而不是直接将值设置给公共变量。以下是一个示例解决方法:

  1. 创建一个服务(service)来共享数据:
import { Injectable } from '@angular/core';

@Injectable({
  providedIn: 'root'
})
export class DataService {
  public sharedValue: any;

  constructor() { }
}
  1. 在需要共享数据的组件中注入该服务:
import { Component } from '@angular/core';
import { DataService } from '路径/data.service';

@Component({
  selector: 'app-example',
  templateUrl: './example.component.html',
  styleUrls: ['./example.component.css']
})
export class ExampleComponent {

  constructor(private dataService: DataService) { }

  setValue(value: any) {
    this.dataService.sharedValue = value;
  }
}
  1. 在其他需要访问该共享数据的组件中再次注入该服务,并使用该服务中的共享变量:
import { Component } from '@angular/core';
import { DataService } from '路径/data.service';

@Component({
  selector: 'app-another-example',
  templateUrl: './another-example.component.html',
  styleUrls: ['./another-example.component.css']
})
export class AnotherExampleComponent {
  sharedValue: any;

  constructor(private dataService: DataService) { }

  getValue() {
    this.sharedValue = this.dataService.sharedValue;
  }
}

通过使用服务来共享数据,可以避免直接设置公共变量的问题,并且可以在整个应用程序中共享数据和状态。

相关内容

热门资讯

我来教大家!老友广东辅助器,h... 我来教大家!老友广东辅助器,hhpoker有没有作弊辅助,爆料教程(有挂分析);【福星临门,好运相随...
推荐攻略!四川途游小程序辅助软... 推荐攻略!四川途游小程序辅助软件,newpoker怎么安装脚本,wpk教程(有挂秘笈);1、点击下载...
推荐攻略!唯思竞技游戏辅助,a... 您好,唯思竞技游戏辅助这款游戏可以开挂的,确实是有挂的,需要了解加微【136704302】很多玩家在...
研究成果!微信小游戏辅助器,如... 研究成果!微信小游戏辅助器,如何下载wepoker安装包,力荐教程(有挂详细);相信小伙伴都知道这个...
教学盘点!宝宝浙江游戏脚本,w... 教学盘点!宝宝浙江游戏脚本,wepoker有透视吗,辅助教程(有挂技巧);1、让任何用户在无需AI插...
记者揭秘!微信小程序微乐辅助器... 记者揭秘!微信小程序微乐辅助器破解版,wepoker能不能透视,第三方教程(有挂细节);运微信小程序...
终于清楚!佛手在线破解,wep... 您好:wepoker手机版透视脚本这款游戏可以开挂的,确实是有挂的,很多玩家在这款游戏中打牌都会发现...
重大消息!广东雀神智能插件辅助... 重大消息!广东雀神智能插件辅助脚本,We poker辅助器下载,wepoke教程(有挂方针)相信很多...
最新技巧!爱玩联盟辅助软件视频... 最新技巧!爱玩联盟辅助软件视频讲解,破解辅助插件wepoker,黑科技教程(有挂助手)相信很多朋友都...
玩家攻略!闲逸亲友圈app辅助... 玩家攻略!闲逸亲友圈app辅助,wpk透视工作室,专业教程(有挂技巧);最新版2026是一款经典耐玩...