组件:
组件是vue最强大的功能之一,组件化开发,
组件可以扩展html元素,封装可重用的代码。
组件之间的传值:①:父传子 用属性传 ;②:子传父:用事件传;③:同级之间传值
组件
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<div id="itany">
<name></name>
<names></names>
</div>
<script src="vue.js"></script>
<script>
//全局
/*Vue.component('name',{
template:`
<ul>
<li>aaaaaa</li>
<li>bbbbbb</li>
<li>cccccc</li>
</ul>
`
})*/
//局部
new Vue({
el:'#itany',
components:{
'names':{
template:`
<ul>
<li>111111</li>
<li>222222</li>
<li>333333</li>
</ul>
`
}
}
})
</script>
</body>
</html>
屏幕展示:点击出现弹出框
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<div id="it">
<name></name>
</div>
<script src="vue.js"></script>
<script>
Vue.component('name',{
template:`
<div>
<h1>{{msg}}</h1>
<button @click='alt'>点击</button>
</div>
`,
data:function(){
return{
msg:'asd'
}
},
methods:{
alt:function(){
alert(123)
}
}
})
new Vue({
el:'#it'
})
</script>
</body>
</html>
屏幕展示:组件传值
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<div id="it">
<one></one>
<two></two>
</div>
<script src="vue.js"></script>
<script>
Vue.component('one',{
template:`
<div>
<h1>我是老大</h1>
<two v-bind:baby=msg></two>
</div>
`,
data:function(){
return{
msg:'hahahaha'
}
}
})
Vue.component('two',{
props:['baby'],
template:`
<div>
<h3>我是老二</h3>
<p>{{baby}}</p>
</div>
`
})
new Vue({
el:'#it'
})
</script>
</body>
</html>
屏幕展示:<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<div id="it">
<one></one>
</div>
<script src="vue.js"></script>
<script>
Vue.component('one',{
template:`
<div>
<two v-bind:a=title></two>
<three v-bind:b=arr></three>
</div>
`,
data:function(){
return{
arr:['dog','cat','pig'],
title:"动物"
}
}
})
Vue.component('two',{
props:['a'],
template:`
<h2>{{a}}</h2>
`
})
Vue.component('three',{
props:['b'],
template:`
<ul>
<li v-for='value in b'>{{value}}</li>
</ul>
`
})
new Vue({
el:'#it'
})
</script>
</body>
</html>
屏幕展示:点击添加
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<div id="it">
<one></one>
</div>
<script src="vue.js"></script>
<script>
Vue.component('one',{
template:`
<div>
<input v-model='txt'>
<button @click='a'>添加</button>
<two v-bind:tian='arr'></two>
</div>
`,
data:function(){
return{
arr:['吃饭','睡觉','打游戏'],
txt:''
}
},
methods:{
a:function(){
this.arr.push(this.txt)
this.txt=""
}
}
})
Vue.component('two',{
props:['tian'],
template:`
<ul>
<li v-for='value in tian'>{{value}}</li>
</ul>
`
})
new Vue({
el:'#it'
})
</script>
</body>
</html>
屏幕展示:9.20
添加删除
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<div id="it">
<one></one>
</div>
<script src="vue.js"></script>
<script>
Vue.component('one',{
template:`
<div>
<input v-model='m'>
<button @click='alt'>添加</button>
<two v-bind:tian=arr></two>
</div>
`,
data:function(){
return{
arr:['逛街','吃饭','看电影'],
m:''
}
},
methods:{
alt:function(){
this.arr.push(this.m)
},
}
})
Vue.component('two',{
props:['tian'],
template:`
<ul>
<li v-for='(value,index) in tian'>{{value}}
<button @click='add(index)'>删除</button></li>
</ul>
`,
methods:{
add:function(e){
this.tian.splice(e,1)
}
}
})
new Vue({
el:'#it'
})
</script>
</body>
</html>
屏幕展示:
点击按钮添加或删除
购物车
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<link rel='stylesheet' href="bootstrap.css">
</head>
<body>
<div id="app">
<one></one>
</div>
<script src="vue.js"></script>
<script>
Vue.component('one',{
template:`
<div class="containter">
<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>
<two v-bind:list='Flist'></two>
</table>
</div>
`,
data:function(){
return{
Flist:[
{name:'apple',price:1,count:2,sum:2},
{name:'banana',price:2,count:3,sum:6},
{name:'orange',price:3,count:4,sum:12}
]
}
}
})
Vue.component('two',{
props:['list'],
template:`
<tbody>
<tr v-for='(value,index) in list'>
<td>{{index+1}}</td>
<td>{{value.name}}</td>
<td>{{value.price}}</td>
<td>
<button @click='redu(index)'>-</button>
<span>{{value.count}}</span>
<button @click='add(index)'>+</button>
</td>
<td>{{value.sum}}</td>
</tr>
<tr>
<td colspan=5>总价:{{suu}}</td>
</tr>
</tbody>
`,
data:function(){
return{
suu:0
}
},
methods:{
redu:function(ind){
if(this.list[ind].count>1){
this.list[ind].count--;
}
this.list[ind].sum=this.list[ind].price*this.list[ind].count;
this.countsuu();
},
add:function(ind){
this.list[ind].count++;
this.list[ind].sum=this.list[ind].price*this.list[ind].count;
this.countsuu();
},
countsuu:function(){
for(var i=0,total=0;i<this.list.length;i++){
total+=this.list[i].sum;
}
this.suu=total;
}
}
})
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">
<one></one>
</div>
<script src="vue.js"></script>
<script>
Vue.component('one',{
template:`
<div>
<h1>{{mess}}</h1>
<two @send='b'></two>
</div>
`,
data:function(){
return{
mess:''
}
},
methods:{
b:function(mess1){
this.mess=mess1
}
}
})
Vue.component('two',{
template:`
<button @click='a'>点击出现</button>
`,
data:function(){
return{
msg:'子传父'
}
},
methods:{
a: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>
</head>
<body>
<div id="app">
<one></one>
</div>
<script src='vue.js'></script>
<script>
Vue.component('one',{
template:`
<div>
<h2>{{as}}</h2>
<h1>{{asd}}</h1>
<two @send="qwe"></two>
</div>
`,
data:function(){
return{
as:'sss',
asd:""
}
},
methods:{
qwe:function(ind){
this.asd=ind
}
}
})
Vue.component('two',{
template:`
<div>
<input type='text' v-model='txt'>
<button @click='a'>点击</button>
</div>
`,
data:function(){
return{
msg:'',
txt:''
}
},
methods:{
a:function(){
this.msg=this.txt
this.$emit('send',this.txt)
}
}
})
new Vue({
el:'#app'
})
</script>
</body>
</html>
屏幕展示: