Vue2.0利用 v-model 实现组件props双向绑定的优美解决方案
作者:tangolivesky 时间:2024-05-02 17:09:28
在项目中开始使用vue2来构建项目了,跟 vue1 很大的一处不同在于2 取消了props 的双向绑定,改成只能从父级传到子级的单向数据流,初衷当然是好的,为了避免双向绑定在项目中容易造成的数据混乱。
解决方案一
然后开始参考网上和github上的方案等等,发现很多解决方案是这样的
用data对象中创建一个props属性的副本
watch props属性 赋予data副本 来同步组件外对props的修改
watch data副本,emit一个函数 通知到组件外
这里以最常见的 modal为例子:modal挺合适双向绑定的,外部可以控制组件的 显示或者隐藏,组件内部的关闭可以控制 visible属性隐藏,同时visible 属性同步传输到外部
///modal.vue 组件
<template>
<div class="modal" v-show="visible">
<div class="close" @click="cancel">X</div>
</div>
</template>
<script>
export default {
name:'modal',
props: {
value: {
type: Boolean,
default:false
}
},
data () {
return {
visible:false
}
},
watch:{
value(val) {
console.log(val);
this.visible = val;
},
visible(val) {
this.$emit("visible-change",val);
}
},
methods:{
cancel(){
this.visible = false;
}
},
mounted() {
if (this.value) {
this.visible = true;
}
}
}
</script>
///调用modal组件
<modal :value="isShow" @visible-change="modalVisibleChange"></modal>
export default {
name: 'app',
data () {
return {
isShow:true,
}
},
methods:{
modalVisibleChange(val){
this.isShow = val;
}
}
}
这样就解决了 组件props 双向绑定的问题。 但是这样有一个不是太美观的现象就是 在父级调用 modal组件的时候,还需要写一个 modalVisibleChange 的methods. 总是显得这部分代码是多余的。 特别是写一个让别人用的公共组件,这样调用太麻烦了。能不能不写method来实现props的双向绑定呢,答案是可以的。
优美解决方案
那就是利用 v-model, 在组件内部放置一个 隐藏的input 控件来保存v-model的值,进行双向绑定
改成如下代码:
<template>
<div class="modal" v-show="visible">
<div class="close" @click="cancel">X</div>
<input type="text" :value="value" style='display:none;'>
</div>
</template>
<script>
export default {
props: {
value: {
type: Boolean,
default:false
}
},
data () {
return {
visible:false
}
},
watch:{
value(val) {
console.log(val);
this.visible = val;
},
visible(val) {
this.$emit('input', val);
}
},
methods:{
cancel(){
this.visible = false;
}
},
mounted() {
if (this.value) {
this.visible = true;
}
}
}
</script>
///调用modal组件
<modal v-model="isShow"></modal>
export default {
name: 'app',
data () {
return {
isShow:false
}
}
}
</script>
这样调用组件的代码是不是很简洁,其他人员要调用的话,会很轻松,只要设置 isShow 就可以控制 modal 组件的显示或者隐藏,同时 如果是modal 组件内部关闭按钮关闭的,状态也会传到 isShow。
来源:https://segmentfault.com/a/1190000008662112
标签:vue,props
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
pandas 数据索引与选取的实现方法
2021-07-09 17:37:44
vue项目使用.env文件配置全局环境变量的方法
2024-04-26 17:37:25
![](https://img.aspxhome.com/file/2023/7/133177_0s.png)
用OpenCV进行年龄和性别检测的实现示例
2021-02-17 18:18:19
![](https://img.aspxhome.com/file/2023/9/133469_0s.png)
SQL查询语句求出用户的连续登陆天数
2024-01-17 22:00:40
![](https://img.aspxhome.com/file/2023/9/129869_0s.jpg)
python使用pandas读写excel文件的方法实例
2021-04-09 17:44:26
![](https://img.aspxhome.com/file/2023/9/95229_0s.png)
PyQt5高级界面控件之QTableWidget的具体使用方法
2023-09-05 15:36:41
![](https://img.aspxhome.com/file/2023/0/72240_0s.png)
mysql5.6.19下子查询为什么无法使用索引
2024-01-15 01:04:29
![](https://img.aspxhome.com/file/2023/1/72511_0s.png)
太有才了!让人称绝的404错误页面
2007-08-19 15:51:00
![](https://img.aspxhome.com/file/uploadpic/20078/19/200781915585830.jpg)
这三个好用的python函数你不能不知道!
2023-04-23 10:29:22
![](https://img.aspxhome.com/file/2023/8/107868_0s.png)
python内存动态分配过程详解
2023-10-02 14:58:46
![](https://img.aspxhome.com/file/2023/8/62918_0s.png)
Python图像增强imgaug详解
2022-02-07 17:07:40
![](https://img.aspxhome.com/file/2023/6/80566_0s.jpg)
Webpack4 使用Babel处理ES6语法的方法示例
2023-08-30 08:12:37
![](https://img.aspxhome.com/file/2023/8/56198_0s.png)
在Python的web框架中编写创建日志的程序的教程
2021-11-25 05:14:07
![](https://img.aspxhome.com/file/2023/4/67244_0s.png)
SQL开窗函数的具体实现详解
2024-01-26 12:16:37
![](https://img.aspxhome.com/file/2023/6/86876_0s.png)
uniapp开发打包成H5部署到服务器的详细步骤
2024-04-10 16:20:19
![](https://img.aspxhome.com/file/2023/1/136701_0s.jpg)
python实现登录与注册系统
2022-04-26 02:32:38
![](https://img.aspxhome.com/file/2023/8/68328_0s.jpg)
AJAX缓存的问题解决办法
2009-04-26 14:47:00
根据选择的checkbox列出选择的值
2008-07-30 12:56:00
![](https://img.aspxhome.com/file/UploadPic/20087/30/200873013437517s.jpg)
动态导航设计
2008-09-21 13:40:00
![](https://img.aspxhome.com/file/UploadPic/20089/21/2008921134132724s.jpg)
浅谈Python数据处理csv的应用小结
2021-06-10 01:40:41
![](https://img.aspxhome.com/file/2023/6/75206_0s.png)