🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
~~~ <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>父子组件传值</title> <script src="vue.js"></script> </head> <body> <!--父组件通过绑定属性的方式向子组件传值--> <!--子组件通过向外触发的方式向父组件传值--> <!--父子组件间传值是单向数据流方式--> <!--子组件不能直接修改父组件的数据--> <div id="root"> <counter :count="3" @inc="handleInc"></counter> <counter :count="2" @inc="handleInc"></counter> <div>{{total}}</div> </div> <script> var counter = { props: ['count'], data() { return { number: this.count } }, template: '<div @click="handleClick">{{number}}</div>', methods: { handleClick() { // this.number++ // this.$emit('inc', 1) this.number += 2 this.$emit('inc', 2) } } } var vm = new Vue({ el: '#root', data: { total: 5 }, components: { counter: counter }, methods: { handleInc(step) { this.total += step } } }) </script> </body> </html> ~~~