regularjs/regular

$watch([object], callback) 会报错:there may a circular dependencies reaches

Closed this issue · 0 comments

A组件:

const A = Regular.extend({
  name: 'A',
  template: `Hello {bindObject.firstName} {bindObject.lastName}, you are {bindObject.age} years old! You like {bindValue}.
<PersonInfo value={bindObject}></PersonInfo>
<InterestSelect value={bindValue}></InterestSelect>`,
  config(data) {
    this.supr();
    Object.assign(data, {
      bindObject: {
        firstName: 'Jack',
        lastName: 'Jones',
        age: 23,
      },
      bindValue: undefined,
    });
    this.$watch(['bindObject', 'bindObject2'], value => {
      console.log(`one of bind objects changed: ${JSON.stringify(value)}`);
    });
  }
});

使用 const a = new A().$inject(document.body) 将组件A的实例插入页面后,会在初始化过程中报错:
Uncaught Error: there may a circular dependencies reaches

原因在于,源码中的 src/helper/watcher.js:L32 使用了 _clone 来创建watch对象的初始值,对象字面量会被重复创建新的对象,使得digest过程无法稳定下来,抛出错误。

解决方案:使用原始对象引用