組件:
組件是vue最強(qiáng)大的功能之一泪酱,組件化開發(fā)钓觉,
組件可以擴(kuò)展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>
屏幕展示:1.png
點(diǎn)擊出現(xiàn)彈出框
<!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'>點(diǎn)擊</button>
</div>
`,
data:function(){
return{
msg:'asd'
}
},
methods:{
alt:function(){
alert(123)
}
}
})
new Vue({
el:'#it'
})
</script>
</body>
</html>
屏幕展示:2.png
組件傳值
<!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>
屏幕展示:3.png
<!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>
屏幕展示:1.png
點(diǎn)擊添加
<!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>
屏幕展示:2.png
3.png
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>
屏幕展示:
點(diǎn)擊按鈕添加或刪除
4.png
購物車
<!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'>數(shù)量</th>
<th class='text-center'>總計(jì)</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>
屏幕展示:5.png
組件子傳父
<!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'>點(diǎn)擊出現(xiàn)</button>
`,
data:function(){
return{
msg:'子傳父'
}
},
methods:{
a:function(){
this.$emit('send',this.msg)
}
}
})
new Vue({
el:'#app'
})
</script>
</body>
</html>
屏幕展示:
點(diǎn)擊按鈕出現(xiàn)
7.png
6.png
子傳父點(diǎn)擊添加
<!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'>點(diǎn)擊</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>
屏幕展示:
輸入點(diǎn)擊添加
8.png
9.png