vue/10-组件化开发/08-父子组件的访问/父组件访问子组件($ref-$children)/练习:计数器.html

57 lines
1.2 KiB
HTML

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<div id="app">
<apps ref="show"></apps>
<button @click="add()">+</button>
<button @click="remove()">-</button>
</div>
<template id="apps">
<div>
<h1>{{num}}</h1>
</div>
</template>
</body>
<script src="../../JavaScript/vue.js"></script>
<script>
// this.$children.内容 那取的是所有组件,如果需要那制定的组件需要加一个下标
// this.$ref.自定义名称.内容 通过组件标签上的ref=“名称”拿取标签,不会因为组件的增加而改变位置
new Vue({
el:"#app",
methods:{
add(){
// this.$refs.show.num+=1
this.$children[0].num+=1
},
remove(){
this.$refs.show.num-=1
}
},
components:{
'apps':{
template:"#apps",
data(){
return {
num:0
}
},
props:{
'app':{
type: Number,
default() {
return -1
}
}
}
}
}
})
</script>
</html>