角度信号效果,尝试重置表单并得到“计算或效果中不允许写入信号”

问题描述 投票:0回答:1

我有角度 17 信号效果的问题。当设施信号更新时,我尝试重置表单。

我在浏览器控制台中出现此消息错误:

NG0600:默认情况下,不允许在

computed
effect
中写入信号。使用
allowSignalWrites
中的
CreateEffectOptions
启用此内部效果。

如果我从效果中删除 this_form.重置,控制台没有错误。如果我将效果选项添加到allowSignalWrites,它就可以工作,但我不明白为什么因为我不写信号,所以我只是重置了一个表单。

formResetEffect = effect(() => {
if (this.facilities()) {
    this._form.reset({
        disabled: false,
        value: null
    });
}}, {allowSignalWrites: true});

你能帮我理解这种行为吗?

谢谢

angular angular-signals
1个回答
0
投票

问题无法重现,请找到参考stackblitz,请修改并分享回来

import { Component, signal, effect } from '@angular/core';
import { ReactiveFormsModule, FormGroup, FormControl } from '@angular/forms';
import { bootstrapApplication } from '@angular/platform-browser';
import 'zone.js';

@Component({
  selector: 'app-root',
  standalone: true,
  imports: [ReactiveFormsModule],
  template: `
    <h1>Hello from {{ name }}!</h1>
    <a target="_blank" href="https://angular.dev/overview">
      Learn more about Angular
    </a>
    <button (click)="setSignal()">asdf</button>
  `,
})
export class App {
  facilities = signal('test');
  name = 'Angular';
  _form: FormGroup = new FormGroup({
    test: new FormControl(null),
  });
  formResetEffect: any;

  constructor() {
    console.log('initialized');
    this.formResetEffect = effect(() => {
      if (this.facilities()) {
        console.log('reseting');
        this._form.reset({
          disabled: false,
          value: null,
        });
      }
    });
  }

  setSignal() {
    this.facilities.set('some new value!');
  }
}

bootstrapApplication(App);

Stackblitz 演示

© www.soinside.com 2019 - 2024. All rights reserved.