1. 父传子
<template>
<div>
<h2>{{message}}</h2>
</div>
</template>
<script>
import {
defineComponent
} from 'vue'
export default defineComponent({
name: 'Children',
components: {},
props: {
message: {
type: String,
default: ''
},
toData: {
type: Object,
default: () => {
return {}
}
},
setup (props, {attrs}) {
const useData = toRef(props.toData, 'a')
console.log('a', useData.value)
useData.value++
console.log('is a ', getA.value)
const { toData } = toRefs(props)
console.log('toData', toData.value)
return {
}
}
})
</script>
<template>
<div class="Parent">
<h1>This is an about page</h1>
<Children :message="messText" :toData="currentData"></Children>
</div>
</template>
<script>
import Children from '@/components/Children.vue'
export default ({
name: 'Parent',
components: {
Children
},
setup () {
const messText = 'this is children'
const currentData = reactive({
a: '11',
b: '22',
c: '33'
})
return {
messText,
currentData
}
}
})
</script>
2. 子传父
<template>
<div>
<button @click="setData">传递到父组件</button>
</div>
</template>
<script>
import {
defineComponent,
getCurrentInstance
} from 'vue'
export default defineComponent({
name: 'Children',
components: {},
props: {
},
emits: ['getData', 'msg'],
setup (props, {emit}) {
const { proxy } = getCurrentInstance()
const setData = () => {
emit('getData', '想要传递的数据')
}
return {
setData
}
}
})
</script>
<template>
<div class="Parent">
<Children @getData="isGetData"></Children>
</div>
</template>
<script>
import Children from '@/views/Children.vue'
export default {
name: 'Parent',
components: {
Children
},
setup () {
const isGetData = (data) => {
console.log('isGetData', data)
}
return {
isGetData
}
}
}
</script>
3.子组件修改父组件传来的值
- 子父组件最常用的通信方式就是通过props进行数据传递,props值只能在父组件中更新并传递给子组件。props是只读的,不能修改,这样做是为了保证数据单向流通。但想要在子组件中修改父组件中的值,该如何做?
子组件接受父组件传来的message并且想要修改,可以通过$emit触发自定义事件来修改该值
<template>
<div>
TestSyncChild-------{{ message }}
<my-button @click="handleClick">
修改
</my-button>
</div>
</template>
<script>
props: {
message: {
type: String,
default: ''
}
},
emits: ['update:message'],
setup () {
const { proxy } = getCurrentInstance()
const handleClick = () => {
proxy.$emit('update:message', '12321')
console.log('emit', proxy)
}
return {
handleClick
}
}
</script>
<div>
<TestSyncChild
:message="message"
@update:message="modifyData"
/>
</div>
import TestSyncChild from '@/modules/Test/testSyncChild.vue'
<script>
export default defineComponent({
name: 'Xx',
components: {
TestSyncChild
},
props: {},
setup () {
const store = useStore()
const { ctx } = getCurrentInstance()
console.log(ctx)
const message = ref('000')
console.log('mes', message)
const modifyData = (data) => {
message.value = data
}
return {
message,
modifyData
}
}
})
</script>
- 可以通过
sync修饰符简写 (2.x写法)
sync 修饰符的作用就是实现父子组件数据的双向绑定,简化功能逻辑代码
sync 修饰符可以实现多个参数的数据双向绑定
<TestSyncChild
:message="message"
@update:message="modifyData"
/>
<TestSyncChild
:message.sync="message"
/>
<info :a.sync="value1" :b.sync="value2" :c.sync="value2" :d.sync="value2"></info>
<info v-bind.sync="obj"></info>
<script>
data() {
obj: {a: '', b: '', c: '', d: ''}
}
</script>
- 通过向
v-model 传递一个参数 (3.x写法)
自定义组件上的 v-model 相当于传递了 message prop 并接收抛出的 update:message 事件
现在可以在同一个组件上使用多个 v-model 绑定
<TestSyncChild
v-model:message="message"
/>