在Vue SFC模式中,父元素传值给子组件采用:props
子组件传值到父元素需要用到$emit。
当然,可以用store实现(这里记录一下遇到的坑)
由于没有仔细阅读Vue的文档,遇到了一个最基础的问题,导出程序出不来如图。命名变量采用了框架自带的方法导致子元素内容始终无法传到父级元素。如图:
改之后完整可运行代码如图:(我这里用Uniapp测试用的,当view标签换成html标签之后程序原理一样)
子元素:
<template>
<view>
<view v-for="d in dataAttributes" >
<label >{{d.title}}</label>
<input style=" border-width: 1px; border-color:gray; border-style:solid;"
@input="input" v-model="d.value" />
</view>
</view>
</template>
<script>
export default{
data(){
return {
dataAttributes:[
{title:'123',key:'title',value:'3',type:'img'},
{title:'1236',key:'placeholder',value:''},
{title:'1237',key:'value',value:''}
]
}
},
methods:{
myinput(e){
var t = this;
t.$emit("input",t.dataAttributes)
console.log(JSON.stringify(t.dataAttributes));
// console.log(JSON.stringify(e.detail));
// console.log(JSON.stringify(this.dataAttributes));
}
}
}
</script>
<style>
</style>
父级调用:
<template>
<view>
<mytemp @input="myCallBack"></mytemp>
</view>
</template>
<script>
import mytemp from '@/components/mytemp/mytemp.vue'
export default {
components:{mytemp},
data() {
return {
title: 'Hello'
}
},
onLoad() {
},
methods: {
myCallBack(e){
console.log(JSON.stringify(e))
}
}
}
</script>
<style>
</style>