共1个回答
AD
游客AdtNgf

watch分为模块watch和实例watch,

模块watch只能watch自己模块的key变化

// code in models/foo/watch.js
import { defWatch } from 'concent'
export const 2KeyChange = defWatch((n, o, f)=>{
   const {key1, key2} = n;//收集依赖
   //首次执行不做任何处理,仅用于触发依赖收集(这里根据需要觉得是否return)
   if(f.isFirstCall)return;

    //对应的逻辑
}, {immediate:true})

实例watch能watch自己模块或者已connect的别的模块的key变化

const setup = ctx=>{
  ctx.watch('someKeyChange', (n)=>{//n 表示自己所属模块的状态
       const {key1, key2} = n;//收集依赖
       if(f.isFirstCall)return; 
   });

  //名字随意
  ctx.watch('otherModuleKeyChange', {
       depKeys:['foo/f1', 'foo/f2'], //人工声明foo模块两个key依赖
       fn:(n, o)=>{
           // n ,o 此时代表foo模块的状态
        },
       // immediate: true, //默认false,根据需要设置为true
   });

    //监听其他模块单个key的变化可简写为(让名字包含模块名和key名)
     ctx.watch('foo/f1', {//当foo模块的key1发生变化时
        fn:(n, o)=>{
           // n ,o 此时代表foo模块的状态
        },
       // immediate: true, //默认false,根据需要设置为true
     })
}

更多关于api的说明在这里:https://concentjs.github.io/concent-doc/api/ref-watch

监控computed是什么意思呢?computed watch的触发源泉都是有state来驱动的,state发生了变化驱动对应预定义好的computed或者watch,watch监控的是state变化