Test1 Component:
import { watch } from "@vue/composition-api";
import useFoo from "../hooks/useFoo.js";
export default {
name: "HelloWorld",
props: ["count1"],
setup(props) {
watch(() => {
//will print when props.count1 changed
console.log("Test1 Count1", props.count1);
});
const { foo } = useFoo(props.count1);
return {
foo
};
}
};
useFoo hook:
import { watch, ref } from "@vue/composition-api";
export default function(count1) {
const foo = ref(2);
watch(() => {
//will not print when props.count1 changed
console.log("useFoo count1", count1);
});
return {
foo
};
}
我想复用一些逻辑,但是发现hook中watch不起作用【console.log("Test1 Count1", props.count1);为何没有执行】?
DMOE: https://codesandbox.io/s/head...
先说一下这里为什么不执行
先看一下源码:

readonly
被包装了set
方法,LOCKED
为false
才可以赋值所以如果要赋值可以这样写: