angular学习笔记之监听数据更新(监听服务数据更新)

 附属代码

/*
 * @Descripttion:公共组件存放部分 
 * @version: 
 * @Author: 玉林路扛把子
 * @Date: 2021-09-24 10:11:49
 * @LastEditors: 玉林路扛把子
 * @LastEditTime: 2021-09-26 11:04:51
 */
import { Component, DoCheck, Input, OnChanges, OnInit, SimpleChanges } from '@angular/core';
import { BaseService } from 'apps/dev-app/src/app/service/base-service.service';
import { HdIdentityUserDto, UserApi } from 'packages/accoount-admin/config/plugins/oauth2';
import { BehaviorSubject } from 'rxjs';
interface ConfigJson {
  [key: string]: {
    name: string,
    type: 'select|casc'
    multiple: true,
    /** 字段名 */
    code: string,
    /** 默认值 */
    value?: any
  }
}
@Component({
  selector: 'abp-common',
  templateUrl: './common.component.html',
  styleUrls: ['./common.component.less']
})
export class CommonComponent implements OnInit, DoCheck {
  @Input() configJson: ConfigJson;
  public baseData$ = new BehaviorSubject<boolean>(null);
  state = {
    userList: HdIdentityUserDto,
    baseData: undefined,
  }
  constructor(private baeService: BaseService, private userApi: UserApi,) {

  }
  ngOnInit(): void {
    if (this.baeService.baseData) {
      this.baeService.baseData.subscribe((res) => {
        //console.log('res', res);
      })
    }
  }
  ngDoCheck() {
    if (this.baeService.baseData) {
      this.state.baseData = this.baeService.baseData;
      console.log(' this.baeService.baseData', this.baeService.baseData);
    }
  }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Angular 中,你可以使用 `ngOnChanges` 生命周期钩子或 `Observable` 来监听变量数据的变化。 1. 使用 `ngOnChanges` 生命周期钩子: - 在组件类中实现 `ngOnChanges` 方法,并接收一个 `SimpleChanges` 对象作为参数。 - 在 `ngOnChanges` 方法中,你可以通过访问 `SimpleChanges` 对象获取到变化前后的值,并执行相应的逻辑。 下面是一个示例: ```typescript import { Component, Input, OnChanges, SimpleChanges } from '@angular/core'; @Component({ selector: 'app-my-component', template: ` <p>Previous Value: {{ previousValue }}</p> <p>Current Value: {{ currentValue }}</p> ` }) export class MyComponent implements OnChanges { @Input() myVariable: any; previousValue: any; currentValue: any; ngOnChanges(changes: SimpleChanges) { if (changes.myVariable) { this.previousValue = changes.myVariable.previousValue; this.currentValue = changes.myVariable.currentValue; // 执行其他逻辑... } } } ``` 在上面的示例中,`myVariable` 是一个输入变量,当它的值发生变化时,`ngOnChanges` 方法会被调用,并且我们可以通过 `changes.myVariable` 获取到变化前后的值。 2. 使用 `Observable`: - 在组件中定义一个 `Subject` 或 `BehaviorSubject` 对象来作为可观察对象。 - 在需要监听数据变化的地方,订阅这个可观察对象,并在回调函数中执行相应的逻辑。 下面是一个示例: ```typescript import { Component } from '@angular/core'; import { Subject } from 'rxjs'; @Component({ selector: 'app-my-component', template: ` <p>Current Value: {{ currentValue }}</p> ` }) export class MyComponent { myVariable$ = new Subject<any>(); currentValue: any; constructor() { this.myVariable$.subscribe(value => { this.currentValue = value; // 执行其他逻辑... }); } updateValue(newValue: any) { this.myVariable$.next(newValue); } } ``` 在上面的示例中,`myVariable$` 是一个 `Subject` 对象,我们可以在其他地方调用 `updateValue` 方法来更新变量的值。当值发生变化时,订阅 `myVariable$` 的回调函数会被触发,我们可以在其中获取到最新的值并执行相应的逻辑。 这两种方法各有不同的应用场景,你可以根据实际需求选择合适的方式来监听变量数据的变化。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

玉林路扛把子

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值