Mik*_*i U 1 javascript vue.js vue-component vuejs3
我是测试provide and inject方法。我将datas,del-function在父级中提供,我将动态渲染在子级中使用v-for='data' in datas...
我想要实现的目标是:当我del-function在子项中按下“删除按钮”=> 时,然后datas在父项中获取删除的项目,并datas在父项中provide进行更新。
datas进行视觉更新。v-for重新渲染。 [!!!]但是当我按下“删除按钮”时,datas更新了,但从视觉上看,没有人被删除。
// parent vue file
<template>
<Reslist/>
</template>
<script>
import Reslist from './components/ResList.vue'
export default {
name: "App",
components: {
Reslist
},
provide() {
return {
datas: this.datas,
delData: this.delData,
};
},
data() {
return {
datas: [
{
id: 1,
name: "wawa",
age: "18",
},
{
id: 2,
name: "wmmmfwa",
age: "1128",
},
],
};
},
methods: {
delData(id) {
console.log('delete-id ='+ id);
const newDatas = this.datas.filter( element => element.id !== id);
this.datas = newDatas;
console.log( this.datas);
},
},
};
</script>
<style>
#app {
font-family: Avenir, Helvetica, Arial, sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
text-align: center;
color: #2c3e50;
margin-top: 60px;
}
</style>
Run Code Online (Sandbox Code Playgroud)
// child vue file
<template>
<div v-for='data in datas' :key="data.name">
<h2>{{data.name}}</h2>
<p>{{data.age}}</p>
<button @click='delData(data.id)'>delete</button>
</div>
</template>
<script>
export default {
inject:['datas','delData']
}
</script>
<style scoped>
div{
width: 18.75rem;
margin: 1.25rem auto;
border: solid 1px grey;
padding: 1.25rem;
}
</style>
Run Code Online (Sandbox Code Playgroud)
我知道如何使用 prop 将数据传递给孩子。我只是想知道为什么[提供和注入]不起作用?在 [provide] 中,我已经 [datas = this.datas] ,我的逻辑是否有错误?
小智 5
晚安,兄弟!
我找到了使用计算道具的解决方案......
希望它有帮助!
父 Vue 文件
<template>
<Reslist/>
</template>
<script>
import Reslist from './ResList.vue'
import { computed } from '@vue/reactivity'
export default {
name: "App",
components: {
Reslist
},
provide() {
return {
datas: computed(() => this.datas),
delData: this.delData,
};
},
data() {
return {
datas: [
{
id: 1,
name: "wawa",
age: "18",
},
{
id: 2,
name: "wmmmfwa",
age: "1128",
},
],
};
},
methods: {
delData(id) {
console.log('delete-id ='+ id);
const newDatas = this.datas.filter( element => element.id !== id);
this.datas = newDatas;
console.log(this.datas);
},
},
};
</script>
<style>
#app {
font-family: Avenir, Helvetica, Arial, sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
text-align: center;
color: #2c3e50;
margin-top: 60px;
}
</style>
Run Code Online (Sandbox Code Playgroud)
子文件
<template>
<div v-for='data in datas' :key="data.name">
<h2>{{data.name}}</h2>
<p>{{data.age}}</p>
<button @click='delData(data.id)'>delete</button>
</div>
</template>
<script>
export default {
inject:['datas','delData']
}
</script>
<style scoped>
div{
width: 18.75rem;
margin: 1.25rem auto;
border: solid 1px grey;
padding: 1.25rem;
}
</style>
Run Code Online (Sandbox Code Playgroud)
配置 Main.js 以接受 Computed 属性。
import { createApp } from 'vue'
import App from './App.vue'
const app = createApp(App)
app.config.unwrapInjectedRef = true
app.mount('#app')
Run Code Online (Sandbox Code Playgroud)
此配置的信息:https ://vuejs.org/guide/components/provide-inject.html#working-with-reactivity
| 归档时间: |
|
| 查看次数: |
1729 次 |
| 最近记录: |