添加删除列表
<body>
<div id='app'>
<my-father></my-father>
</div>
<script src='js/js/vue.js'></'></script>
<script>
Vue.componeponent('my-father',{
template:`
<div>
<input type='text' v-model='fruit'> <button @click='add'>添加</button>
<my-child v-bind:fruList='list'></my-child>
</div>
`,
data:function(){
return{
list:['apple','pear','orange'],
fruit:''
}
},
methods:{
add:function(){
this.list.push(thi(sh(this.fruit)
)
}
}
})
Vue.componeponent('my-child',{
props:['fruList'],
template:`
<ul>
<li v-for="(value,index) in fruList">
{{value}}
<button @click='delt(index)'>删除</button>
</li>
</ul>
`,
methods:{
delt:function(ind){
this.fruList.splice(ind(ind,1)
}
}
})
new Vue({
el:'#app'
})
</script>
</body>
购物车
<body>
<div id='app'>
<my-father></my-father>
</div>
<script src='js/js/vue.js'></'></script>
<script>
Vue.componeponent('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:list='fruList'></my-child>
</table>
</div>
`,
data:function(){
return{
fruList:[
{pname:'apple',price:3,count:3,sub:9},
{pname:'pear',price:4,count:4,sub:16},
{pname:'orange',price:5,count:5,sub:25}
]
}
}
})
Vue.componeponent('my-child',{
props:['list'],
template:`
<tbody>
<tr v-for="(value,index) in list">
<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.list[ind[ind].count++ ;
//计算小计
this.list[ind[ind].sub=ub=this.list[ind[ind].count*nt*this.list[ind[ind].price;
this.countSum();
();
},
redu:function(ind){
if(if(this.list[ind[ind].count>1){
this.list[ind[ind].count--
}
//计算小计
this.list[ind[ind].sub=ub=this.list[ind[ind].count*nt*this.list[ind[ind].price;
this.countSum();
();
},
countSum:function(){
for(var i=0,total=0;i<;i<this.list.length;i++;i++){
total+=l+=this.list[i].[i].sub;
}
this.sum=tot=total;
}
}
})
new Vue({
el:'#app'
})
</script>
</body>
子传父
子传父用事件
实例
<body>
<div id='app'>
<my-father></my-father>
</div>
<script src='js/js/vue.js'></'></script>
<script>
Vue.componeponent('my-father',{
template:`
<div>
<h1>{{mess}}</h1>
<my-child @send='revMsg'></my-child>
</div>
`,
data:function(){
return{
mess:''
}
},
methods:{
revMsg:function(txt){
this.mess=txt=txt
}
}
})
Vue.componeponent('my-child',{
template:`
<button @click='sendFather'>给父组件</button>
`,
data:function(){
return{
msg:'张世远是共产主义接班人'
}
},
methods:{
sendFather:function(){
this.$emit('send',d',this.msg)
)
}
}
})
new Vue({
el:"#app"
})
</script>
</body>