问题遇到的现象和发生背景
问题相关代码,请勿粘贴截图
Home.vue
<template>
<el-button @click="filterFromHome()">filterFromHome</el-button>
<Test></Test>
</template>
<script>
import Test from "./Test.vue"
import useTest from "@/hooks/useTest"
import {toRefs} from "vue"
export default {
name: 'Home',
components: {Test},
setup() {
const {filterFromHome} = useTest()
return {
filterFromHome
}
}
}
</script>
<style scoped>
</style>
useTest.js
/**
* Created by baidm in 2022/3/30 on 20:13
*/
import {reactive, watch} from "vue"
export default function () {
let arr = _.map(new Array(100), (item, index) => {
return {
id: index + 1,
name: `test${index + 1}`,
}
})
const info = reactive({
list: arr,
sourceList: arr
})
watch(info, (val, oldVal) => {
info.list = val.list
console.log("useTest.js", val);
})
const filterFromHome = () => {
info.list = _.filter(info.sourceList, item => item.id <= 50)
console.log("Home.vue", info);
}
const filterInfoList = (row) => {
info.list = _.filter(info.sourceList, item => item.id <= row.id)
}
return {
info,
filterInfoList,
filterFromHome
}
}
Test.vue
<template>
<div class="test">
<p v-for="item in info.list" :key="item.id" @click="filterInfoList(item)">
name:{{item.name}}
</p>
</div>
</template>
<script>
import useTest from "@/hooks/useTest"
import {watch, toRefs} from "vue"
export default {
name: "Test",
setup() {
const {info, filterInfoList} = useTest()
watch(info, (val) => {
console.log("Test.vue", val);
})
return {
info,
filterInfoList,
}
}
}
</script>
<style scoped>
.test {
border: 1px solid #333;
}
.test p {
cursor: pointer;
}
</style>
运行结果及报错内容
点击按钮 filterFromHome 后,子组件模板未更新??