组件之间的传值分为三种传值方式
1.父组件传值给子组件:使用属性传 props['自定义属性名']
2.子组件传值给父组件: 使用方法传 $emit
3.非父子组件之间的传值:借助空的vue实例传
一、父组件传值给子组件(1)
<body>
<div id='itany'>
<father></father>
</div>
<script src='js/vue.js'></script>
<script>
Vue.component('father',{
template:`
<div>
<h1>这是父元素</h1>
<child v-bind:number='num'></child>
<button @click='add'>点击加1</button>
</div>
`,
data:function(){
return{
num:1
}
},
methods:{
add:function(){
this.num++;
}
}
})
Vue.component('child',{
props:['number'],
template:`
<div>
<h3>这是子元素</h3>
<a href='#'>{{number}}</a>
</div>
`
})
new Vue({
el:'#itany'
})
</script>
</body>
父组件给子组件传值(2)
<body>
<div id='app'>
<cont></cont>
</div>
<script src='js/vue.js'></script>
<script>
Vue.component('cont',{
template:`
<div>
<h1>这是父元素</h1>
<top-title v-bind:fruTit='tit'></top-title>
<list v-bind:fruList='fruit'></list>
</div>
`,
data:function(){
return{
fruit:['apple','pear','banana'],
tit:'水果列表'
}
}
})
//子组件1
Vue.component('top-title',{
props:['fruTit'],
template:`
<h3>{{fruTit}}</h3>
`
})
//子组件
Vue.component('list',{
props:['fruList'],
template:`
<ul>
<li v-for="value in fruList">{{value}}</li>
</ul>
`
})
new Vue({
el:'#app'
})
</script>
二、使用福组组件传值实现水果列表,如下图,可以实现增加和删除的功能 1.png
<body>
<div id='app'>
<my-component></my-component>
</div>
<script src='js/vue.js'></script>
<script>
Vue.component("my-component",{
template:`
<div>
<input type='text' v-model='list'>
<button @click='add'>添加</button>
<my-child v-bind:fruit='fruList'></my-child>
</div>
`,
methods:{
add:function(){
this.fruList.push(this.list);
this.list=''
}
},
data:function(){
return{
fruList:['apple','pear','banana'],
list:''
}
}
})
Vue.component('my-child',{
props:['fruit'],
template:`
<ul>
<li v-for="(value,index) in fruit">
{{value}}
<button @click='delt(index)'>删除</button>
</li>
</ul>
`,
methods:{
delt:function(ind){
this.fruit.splice(ind,1)
}
}
})
new Vue({
el:'#app'
})
</script>
</body>
三、父传子实现购物车效果
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<link rel="stylesheet" href="css/bootstrap.css">
</head>
<body>
<div id='app'>
<my-father></my-father>
</div>
<script src='js/vue.js'></script>
<script>
Vue.component('my-father',{
template:`
<div class='container'>
<table class='table table-bordered text-center'>
<thead>
<tr>
<th class='text-center'>编号</th>
<th class='text-center'>名称</th>
<th class='text-center'>单价</th>
<th class='text-center'>数量</th>
<th class='text-center'>小计</th>
</tr>
</thead>
<my-child v-bind:fruit='list'></my-child>
</table>
</div>
`,
data:function(){
return{
list:[
{pname:'apple',price:5,count:5,sub:25},
{pname:'pear',price:4,count:4,sub:16},
{pname:'orange',price:3,count:3,sub:9}
]
}
}
})
Vue.component("my-child",{
props:['fruit'],
template:`
<tbody>
<tr v-for="(value,index) in fruit">
<td>{{index+1}}</td>
<td>{{value.pname}}</td>
<td>{{value.price}}</td>
<td>
<button @click='add(index)'>+</button>
<span>{{value.count}}</span>
<button @click="redu(index)">-</button>
</td>
<td>{{value.sub}}</td>
</tr>
<tr>
<td colspan=5>总价:{{sum}}</td>
</tr>
</tbody>
`,
data:function(){
return{
sum:0
}
},
methods:{
add:function(ind){
this.fruit[ind].count++;
this.fruit[ind].sub=this.fruit[ind].count*this.fruit[ind].price;
this.countSum();
},
redu:function(ind){
if(this.fruit[ind].count>1){
this.fruit[ind].count--;
}
this.fruit[ind].sub=this.fruit[ind].count*this.fruit[ind].price;
this.countSum();
},
countSum:function(){
for(var i=0,total=0;i<this.fruit.length;i++){
total+=this.fruit[i].sub;
}
this.sum=total;
}
}
})
new Vue({
el:'#app'
})
</script>
</body>
</html>
网友评论