We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Props
父组件
<template> <div class="parent"> <son :number="num"></son> </div> </template> <script> import son from './children/son' export default { name: 'parent', data () { return { num: 1 } }, components:{ son } } </script>
子组件
<template> <div class="son"> {{number}} </div> </template> <script> export default { name: 'son', props:{ number:Number } } </script>
子组件显示 0
修改父组件
<template> <div class="parent"> <son :number="num"></son> </div> </template> <script> import son from './children/son' export default { name: 'parent', data () { return { num: 1 } }, mounted () { setTimeout(() => { this.num = 99; }, 3000); }, components:{ son } } </script>
子组件先显示 0, 3 秒后显示 99
<template> <div class="parent"> <son :number="num" @reset="resetNum"></son> </div> </template> <script> import son from './children/son' export default { name: 'parent', data () { return { num: 0 } }, mounted () { setTimeout(() => { this.num = 99; }, 3000); }, methods: { resetNum (data) { this.num = data; } }, components:{ son } } </script>
<template> <div class="son"> {{number}} <button @click="reset">reset</button> </div> </template> <script> export default { name: 'son', props:{ number:Number }, methods: { reset () { this.$emit('reset', 0) } } } </script>
此时, 子组件初始显示 0, 3 秒后显示 99, 点击 reset 重新显示 0
reset
实现双向数据绑定的语法糖
<template> <div class="parent"> <son :number.sync="num"></son> </div> </template> <script> import son from './children/son' export default { name: 'parent', data () { return { num: 0 } }, mounted () { setTimeout(() => { this.num = 99; }, 3000); }, components:{ son } } </script>
<template> <div class="son"> {{number}} <button @click="reset">reset</button> </div> </template> <script> export default { name: 'son', props:{ number:Number }, methods: { reset () { this.$emit('update:number', 0) } } } </script>
https://cn.vuejs.org/v2/guide/components.html#Prop
The text was updated successfully, but these errors were encountered:
No branches or pull requests
vue 父子组件通信
父 -> 子的数据传递
使用
Props
传递数据子组件显示 0
动态
Props
子组件先显示 0, 3 秒后显示 99
子 -> 父的数据传递
自定义事件
此时, 子组件初始显示 0, 3 秒后显示 99, 点击
reset
重新显示 0.sync 修饰符
https://cn.vuejs.org/v2/guide/components.html#Prop
The text was updated successfully, but these errors were encountered: