为什么这段代码checkelist数据更新了但是页面渲染没更新<template> <div> <div> <div>封装后的多选框</div> <div> <w-checkbox-group v-model:value="checkedList" :options="plainOptions" /> </div> </div> </div> </template> <script lang="ts"> import WCheckboxGroup from "@/components/w-checkbox-group/checkboxGroup.vue"; import { defineComponent, reactive, toRefs, onUpdated, watch } from "vue"; const plainOptions = [ { label: "Apple", value: "Apple" }, { label: "Pear", value: "Pear" }, { label: "cc", value: "cc" }, ]; export default defineComponent({ components: { WCheckboxGroup }, setup() { const state = reactive({ indeterminate: true, checkAll: false, checkedList: [], }); // const onChangeOption = (val: any) => { // state.checkedList = val; // }; onUpdated(() => { console.log("checkedList changed:", state.checkedList); }); watch( state.checkedList, (newVal) => { console.log("checkedList watch:", newVal); }, { immediate: true } ); return { ...toRefs(state), plainOptions }; }, }); </script>
时间: 2023-06-17 07:08:08 浏览: 92
可能是因为 `checklist` 是响应式数据,而 `watch` 函数默认只会对响应式数据进行浅层比较,如果新旧值的引用地址相同,就不会触发回调函数。可以尝试在 `watch` 函数中手动指定深层比较选项,例如:
```js
watch(
() => state.checkedList,
(newVal, oldVal) => {
console.log("checkedList watch:", newVal);
},
{ deep: true, immediate: true }
);
```
另外,`onUpdated` 钩子只在组件的 VDOM 更新完成后才会触发,因此可能不适用于检测响应式数据的变化。如果希望在 `checkedList` 发生变化时立即得到通知,可以使用 `watch` 函数。
相关问题
请帮我看下这段代码为什么父组件接收到checkedList但是页面没有渲染<template> <div> <div> <div>封装后的多选框</div> <div> <w-checkbox-group v-model:value="checkedList" :options="plainOptions" @changeOption="onChangeOption" /> </div> </div> </div> </template> <script lang="ts"> import WCheckboxGroup from "@/components/w-checkbox-group/checkboxGroup.vue"; import { defineComponent, reactive, toRefs, onUpdated } from "vue"; const plainOptions = [ { label: "Apple", value: "Apple" }, { label: "Pear", value: "Pear" }, { label: "cc", value: "cc" }, ]; export default defineComponent({ components: { WCheckboxGroup }, setup() { const state = reactive({ indeterminate: true, checkAll: false, checkedList: [], }); const onChangeOption = (val: any) => { state.checkedList = val; }; onUpdated(() => { console.log("checkedList changed:", state.checkedList); }); return { ...toRefs(state), plainOptions, onChangeOption }; }, }); </script>
这段代码看起来没有明显的问题,可以尝试检查以下几个方面来确定问题所在:
1. 确保父组件中正确引入了子组件 `WCheckboxGroup`,且引入路径正确。
2. 检查子组件 `WCheckboxGroup` 的实现是否正确,是否正确响应了 `v-model`。
3. 确保父组件中正确绑定了 `options` 和 `changeOption` 事件。
4. 检查父组件中是否正确渲染了子组件。
如果以上步骤都没有问题,可以尝试在父组件中加入一个 `watch` 监听 `checkedList` 的变化,以确保 `checkedList` 确实被修改了,例如:
```js
export default defineComponent({
components: { WCheckboxGroup },
setup() {
const state = reactive({
indeterminate: true,
checkAll: false,
checkedList: [],
});
const onChangeOption = (val: any) => {
state.checkedList = val;
};
onUpdated(() => {
console.log("checkedList changed:", state.checkedList);
});
watch(state.checkedList, (newVal) => {
console.log("checkedList watch:", newVal);
});
return { ...toRefs(state), plainOptions, onChangeOption };
},
});
```
这样可以确保 `checkedList` 确实被修改了,如果仍然无法渲染页面,可以考虑在子组件中加入一些调试信息,例如在子组件的 `mounted` 中输出 `options` 和 `checked`,以确定子组件是否正确响应了 `v-model`,例如:
```js
export default {
name: "WCheckboxGroup",
props: {
value: Array,
options: Array,
},
data: function () {
return {
checked: this.value,
};
},
mounted() {
console.log("options:", this.options);
console.log("checked:", this.checked);
},
watch: {
value: function (newVal) {
this.checked = newVal;
},
checked: function (newVal) {
this.$emit("input", newVal);
},
},
};
```
如果以上步骤仍然无法解决问题,可以提供更多上下文信息或者代码片段,方便排查问题。
帮我看下子组件更细了checklist后父组件的数据为什么渲染不出来<template> <div> <div>封装后的多选框</div> <div> <w-checkbox-group v-model:value="checkedList" :options="plainOptions"> </w-checkbox-group> </div> </div> </template> <script lang="ts"> import WCheckboxGroup from "@/components/w-checkbox-group/checkboxGroup.vue"; import { defineComponent, reactive, toRefs, ref, onUpdated } from "vue"; const plainOptions = [ { label: "Apple", value: "Apple" }, { label: "Pear", value: "Pear" }, { label: "cc", value: "cc" }, ]; export default defineComponent({ components: { WCheckboxGroup, }, setup() { const state = reactive({ indeterminate: true, checkAll: false, checkedList: ["Apple"], }); const onChangeOption = (val: any) => { // const v = val.find((item: any) => !state.checkedList.includes(item)); // state.checkedList = [v]; }; return { ...toRefs(state), plainOptions, onChangeOption, }; }, }); </script>
看起来你的代码中没有使用 `onChangeOption` 方法,所以父组件的数据不会更新。你可以在 `WCheckboxGroup` 子组件中触发 `onChangeOption` 方法来更新父组件的数据。
你可以在 `WCheckboxGroup` 组件中添加一个 `watch` 监听 `value` 的变化,并在变化时触发 `onChangeOption` 方法。修改后的代码如下:
```vue
<template>
<div>
<div v-for="(option, index) in options" :key="index">
<label>
<input type="checkbox" v-model="option.checked" :value="option.value" @change="onChange" />
{{ option.label }}
</label>
</div>
</div>
</template>
<script lang="ts">
import { defineComponent, toRefs, watch } from "vue";
export default defineComponent({
name: "WCheckboxGroup",
props: {
value: {
type: Array,
default: () => [],
},
options: {
type: Array,
default: () => [],
},
},
setup(props, { emit }) {
const onChange = (event: Event) => {
const target = event.target as HTMLInputElement;
const value = target.value;
const checked = target.checked;
let newValue = [...props.value];
if (checked) {
newValue.push(value);
} else {
newValue = newValue.filter((item) => item !== value);
}
emit("update:value", newValue);
};
watch(props.value, (newValue) => {
emit("changeOption", newValue);
});
return {
...toRefs(props),
onChange,
};
},
});
</script>
```
然后在父组件中,你可以监听 `WCheckboxGroup` 组件的 `changeOption` 事件,并在事件处理函数中更新 `checkedList` 的值。修改后的代码如下:
```vue
<template>
<div>
<div>
<div>封装后的多选框</div>
<div>
<w-checkbox-group v-model:value="checkedList" :options="plainOptions" @changeOption="onChangeOption" />
</div>
</div>
</div>
</template>
<script lang="ts">
import WCheckboxGroup from "@/components/w-checkbox-group/checkboxGroup.vue";
import { defineComponent, reactive, toRefs } from "vue";
const plainOptions = [
{ label: "Apple", value: "Apple" },
{ label: "Pear", value: "Pear" },
{ label: "cc", value: "cc" },
];
export default defineComponent({
components: {
WCheckboxGroup,
},
setup() {
const state = reactive({
indeterminate: true,
checkAll: false,
checkedList: ["Apple"],
});
const onChangeOption = (val: any) => {
state.checkedList = val;
};
return {
...toRefs(state),
plainOptions,
onChangeOption,
};
},
});
</script>
```
这样修改后,当子组件中选项的选中状态发生变化时,父组件的 `checkedList` 数据也会更新,并重新渲染视图。