vue中父子组件数据传递
解决在子组件中修改父组件传来的数据,报错如下:
在vue2.0+ 后不再是双向绑定,如果要进行双向绑定需要特殊处理。
[Vue warn]: Avoid mutating a prop directly since the value will be overwritten whenever the parent component re-renders. Instead, use a data or computed property based on the prop's value. Prop being mutated: "你修改的属性名"
由于在vue中数据流是单向的。
方法1 通过事件$emit父组件修改
**父组件中**
<template>
<div>
<el-button type="text" @click="dialogVisible = true">点击打开Dialog</el-button>
<child @closeDialog="getCloseDialog" :visiable="dialogVisible"></child>
</div>
</template>
<script>
//引入子组件
export default {
data() {
return {
dialogVisible: false,
}
},
methods:{
getCloseDialog(value){
this.dialogVisible=value
}
}
}
</script>
**子组件中**