vue子元件控制父元件的資料props
阿新 • • 發佈:2021-02-13
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script> </head> <body> <div id="app"> <Fa /> </div> <!-- 父元件 --> <template id="fa"> <div class="fa"> 爸爸的錢包餘額{{money}} <Son :buy="buy"/> </div> </template> <!-- 子元件 --> <template id="son"> <div class="son"> 熊孩子<button @click="buy">買玩具</button> </div> </template> <script> Vue.component('Fa', { template: "#fa", data () { return { money: 20 } }, methods: { buy() { this.money -= 5 } } }) Vue.component('Son', { template: "#son", props: ['buy'] }) new Vue({ el: '#app', data: { } }) </script> </body> </html>