Vue父组件与子组件之间的传值
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
<title>Document</title>
</head>
<body>
<div id="app">
<div :style="{fontSize:fontSize+'px'}">{{ str }}</div>
<menu-item :str="str" :snum="12" :sbool="true" :sarray="sarray" :sobj="sobj" @enlarge-text="handle"></menu-item>
</div>
<script>
Vue.component('menu-item',{
props: ['str','snum','sbool','sarray','sobj'],
template:
`<div>
<span>{{ str }}</span>
<span>{{ snum+12 }}</span>
<span>{{ sbool }}</span>
<ul>
<li v-for="(item, index) in sarray" :key="index">{{ item }}</li>
</ul>
<div>
<span>{{ sobj.id }}</span>
<span>{{ sobj.name }}</span>
<span>{{ sobj.age }}</span>
</div>
<button v-on:click="sarray.push('e')">点击像数组里加e</button>
<button v-on:click="$emit('enlarge-text')">点击让父组件字体变大</button>
</div>`
})
var vm=new Vue({
el:"#app",
data:{
str:"heall",
sarray:["a","b","c","d"],
sobj:{
id:2,
name:"gg",
age:"20"
},
fontSize:10
},
methods: {
handle:function(){
this.fontSize+=5;
}
},
})
</script>
</body>
</html>