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

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

相关内容

热门资讯

据权威媒体报道!有哪些免费的w... 据权威媒体报道!有哪些免费的wpk作弊码,红龙poker辅助,切实存在有挂(有挂方式)1、玩家可以在...
推荐一款!德普之星辅助软件,w... 推荐一款!德普之星辅助软件,wepoker免费钻石,竟然有挂(有挂方针)1、首先打开辅助器下载最新版...
黑科技教程!pokernow辅... 黑科技教程!pokernow辅助工具,德扑软件,切实存在有挂(有挂细节)1、下载好德扑软件脚本下载之...
科普攻略!wepoker代打辅... 科普攻略!wepoker代打辅助机器人,werplan外卦神器,切实真的是有挂(真的有挂)1、下载好...
近年来!aapoker安装包怎... 近年来!aapoker安装包怎么使用,德普之星透视免费,好像真的是有挂(有挂功能)1、德普之星透视免...
玩家实测!wepoker到底有... 玩家实测!wepoker到底有没有透视,aapoker俱乐部靠谱吗,确实是有挂(有挂细节)亲,关键说...
反观!约局吧开挂,德普之星辅助... 反观!约局吧开挂,德普之星辅助器app,原来真的是有挂(有挂技巧)一、德普之星辅助器app可以开透视...
规律辅助挂!wepoker免费... 规律辅助挂!wepoker免费透视,德普辅助软件,其实存在有挂(有挂方法)1、德普辅助软件免费脚本咨...
一分钟了解!德州之星扫描器,哈... 一分钟了解!德州之星扫描器,哈糖大菠萝可以开挂吗,好像存在有挂(有挂方法)1、首先打开辅助器下载最新...
2026版技巧!werplan... 2026版技巧!werplan怎么作弊,德扑之星有辅助软件吗,果然是有挂(发现有挂)1、任何德扑之星...