<!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>
vue子组件控制父组件的数据props
最新推荐文章于 2023-09-23 15:00:00 发布