<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<div id='app'>
<my-father></my-father>
</div>
<script src='js/vue.js'></script>
<script>
Vue.component("my-father",{
template:`
<div>
<h1>{{mess}}</h1>
<my-child @send='rcvMsg'></my-child>
</div>
`,
data:function(){
return{
mess:''
}
},
methods:{
rcvMsg:function(txt){
this.mess=txt
}
}
})
Vue.component('my-child',{
template:`
<button @click='sendToFather'>传给父元素</button>
`,
data:function(){
return{
msg:'我是子组件中的数据,要给父组件传值'
}
},
methods:{
sendToFather:function(){
// this.$emit('自定义事件名',要传输的数据)
this.$emit('send',this.msg)
}
}
})
new Vue({
el:"#app"
})
</script>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<div id='app'>
<my-father></my-father>
</div>
<script src='js/vue.js'></script>
<script>
Vue.component("my-father",{
template:`
<div>
<h1>我是父组件</h1>
<p>组组件传过来的数据:<b>{{mess}}</b></p>
<my-child @send='rcvMsg'></my-child>
</div>
`,
data:function(){
return{
mess:''
}
},
methods:{
rcvMsg:function(txt){
this.mess=txt
}
}
})
Vue.component('my-child',{
template:`
<div>
<h1>我是子组件</h1>
<input type='text' v-model='msg'> <button @click='sendMsg'>发送</button>
</div>
`,
data:function(){
return{
msg:''
}
},
methods:{
sendMsg:function(){
this.$emit('send',this.msg)
}
}
})
new Vue({
el:'#app'
})
</script>
</body>
</html>
<!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>
2018-09-20子传父的案例
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 尖子生要想成为尖子生里的尖子,必须做到这一点! 尖子生特别是在现在中高考简单的情况下,发挥失误一点,就可能被别人超...