Vue——组件篇
author:木子六日
学习视频来源于
coderwhy老师的vue教学
01.组件的基本使用
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>组件的基本使用</title>
</head>
<body>
<div id="app">
<cpn></cpn>
</div>
<script src="js/vue.js"></script>
<script type="text/javascript">
const cpn = Vue.extend({
template:`
<div>
<h1>标题</h1>
<p>哈哈哈哈哈哈</p>
<p>呵呵呵呵呵呵</p>
</div>
`
})
const app = new Vue({
el:'#app',
data:{
},
components:{
cpn: cpn
}
})
</script>
</body>
</html>
02.父组件与子组件
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>父组件与子组件</title>
</head>
<body>
<div id="app">
<cpn2></cpn2>
</div>
<script src="js/vue.js"></script>
<script type="text/javascript">
const cpn1 = Vue.extend({
template:`
<div>
<h1>我是组件1标题</h1>
<p>哈哈哈哈</p>
</div>
`
});
const cpn2 = Vue.extend({
template:`
<div>
<cpn1></cpn1>
<h1>我是组件2标题</h1>
<p>呵呵呵呵</p>
</div>
`,
components:{
cpn1:cpn1
}
});
const app = new Vue({
el:'#app',
components:{
cpn2:cpn2
}
})
</script>
</body>
</html>
03.组件注册的语法糖
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>组件注册的语法糖</title>
</head>
<body>
<div id="app">
<cpn1></cpn1>
<cpn2></cpn2>
</div>
<script type="text/template" id="cpn1">
<div>
<h1>我是组件1标题</h1>
<p>{{haha}}</p>
</div>
</script>
<template id="cpn2">
<div>
<h1>我是组件2标题</h1>
<p>{{hehe}}</p>
</div>
</template>
<script src="js/vue.js"></script>
<script type="text/javascript">
Vue.component('cpn2',{
template:'#cpn2',
data(){
return {
hehe:'呵呵呵呵'
}
}
})
const app = new Vue({
el:'#app',
components:{
cpn1:{
template:'#cpn1',
data(){
return {
haha:'哈哈哈哈'
}
}
}
}
})
</script>
</body>
</html>
04.父子组件间的通信(父传子)
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>父子组件间的通信</title>
</head>
<body>
<div id="app">
<cpn :cwords="words"></cpn>
</div>
<template id="cpn">
<div>
{{cwords}}
</div>
</template>
<script src="js/vue.js"></script>
<script type="text/javascript">
const app = new Vue({
el:'#app',
data:{
words:['啊啊啊','哈哈哈','呵呵呵']
},
components:{
cpn:{
template:'#cpn',
props:{
cwords:{
type: Array,
default(){
return ['a','b','c']
}
}
}
}
}
})
</script>
</body>
</html>
05.父子组件间的通信(子传父)
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>父子组件通信</title>
</head>
<body>
<div id="app">
<cpn @btnclick="cpnclick"></cpn>
<h2>{{temp}}</h2>
</div>
<template id="cpn">
<div>
<button v-for="item in categories" @click="itemclick(item)">{{item.name}}</button>
</div>
</template>
<script src="js/vue.js"></script>
<script type="text/javascript">
const app = new Vue({
el:'#app',
data:{
temp:''
},
methods:{
cpnclick(item){
this.temp = item.name
}
},
components:{
cpn:{
template:'#cpn',
data(){
return {
categories:[
{id:'1',name:'热门推荐'},
{id:'2',name:'家用电器'},
{id:'3',name:'手机数码'},
{id:'4',name:'电脑办公'}
]
}
},
methods:{
itemclick(item){
this.$emit('btnclick',item)
}
}
}
}
})
</script>
</body>
</html>
06.父组件访问子组件
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>父组件访问子组件</title>
</head>
<body>
<div id="app">
<cpn ref="aaa"></cpn>
<button type="button" @click="btnClick">按钮</button>
</div>
<template id="cpn">
<div>
<h1>我是子组件</h1>
</div>
</template>
<script src="js/vue.js"></script>
<script type="text/javascript">
const app = new Vue({
el:'#app',
methods:{
btnClick(){
console.log(this.$children[0].name);
console.log(this.$children[0].showName());
console.log(this.$refs.aaa.name)
console.log(this.$refs.aaa.showName())
}
},
components:{
cpn:{
template:'#cpn',
data(){
return {
name:'我是子组件的name属性'
}
},
methods:{
showName(){
return this.name;
}
}
}
}
})
</script>
</body>
</html>
07.子组件访问父组件
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>子组件访问父组件</title>
</head>
<body>
<div id="app">
<cpn></cpn>
</div>
<template id="cpn">
<button type="button" @click="btnClick">按钮</button>
</template>
<script src="js/vue.js"></script>
<script type="text/javascript">
const app = new Vue({
el:'#app',
data:{
message:'我是父组件的属性'
},
components:{
cpn:{
template:'#cpn',
methods:{
btnClick(){
console.log(this.$parent.message);
console.log(this.$root.message);
}
}
}
}
})
</script>
</body>
</html>
08.slot插槽的基本使用
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>slot的基本使用</title>
</head>
<body>
<div id="app">
<cpn></cpn>
<cpn><span>我是span</span></cpn>
<cpn><p>我是p</p></cpn>
<cpn></cpn>
<hr >
<cpn2><span slot="mid">我是插槽2</span></cpn2>
<hr >
<cpn3>
<template v-slot="slot">
<span v-for="item in slot.data">{{item}}</span>
</template>
</cpn3>
</div>
<template id="cpn">
<div>
<h1>我是子组件</h1>
<slot><button>按钮</button></slot>
</div>
</template>
<template id="cpn2">
<div>
<slot><button>我是插槽1</button></slot>
<slot name="mid"><button>我是插槽2</button></slot>
<slot><button>我是插槽3</button></slot>
</div>
</template>
<template id="cpn3">
<div>
<slot :data="languages">
</slot>
</div>
</template>
<script src="js/vue.js"></script>
<script type="text/javascript">
const app = new Vue({
el:'#app',
components:{
cpn:{
template:'#cpn'
},
cpn2:{
template:'#cpn2'
},
cpn3:{
template:'#cpn3',
data(){
return {
languages:['C','C++','Java','Python','Go']
}
}
}
}
})
</script>
</body>
</html>