上一章博客我们讲解了Vue生命周期,列表过滤,计算属性和监听器
这一章我们来讲Vue组件开发
创建组件两种方式
var Header = {
template:'模板' ,
data是一个函数,
methods:功能,
components:子组件们
}//局部声明
Vue.component('组件名',组件对象);//全局注册 等于注册加声明了
组件的分类
组件开发三部曲:声明、注册、使用
注意:子组件的命名,如果有驼峰命名,在使用子组件标签时用“-”隔开
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<div id="app">
大家好,{{name}}
<hello></hello>
<saybyebye></saybyebye>
</div>
<template id="myhello">
<div>hello,{{name}}</div>
</template>
</body>
<script src="../js/vue2.7.js"></script>
<script>
// 注册了一个全局组件,名字叫hello
Vue.component('hello',{
template:`#myhello`,
data(){
return{
name:'我是hello'
}
}
})
// 定义一个局部组件
var saybyebye={
template:`你好`
}
var app = new Vue({
el:'#app',
data(){
return{
name:"张三",
}
},
// 注册局部组件
components:{
saybyebye
}
})
</script>
</html>
slot就是在声明子组件时给DOM留下的坑位,以便于父组件在使用子组件的时候可以在坑位里动态的插入自己的内容。
并且,坑位是可以命名的,也就是说,子组件在声明的时候命名坑位,方便父组件在指定的坑位中插入内容
slot是动态的DOM
匿名插槽
- 匿名插槽就是在声明的时候没有声明name,会把全部内容都显
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<div id="app">
大家好,{{name}}
<hello></hello>
<saybyebye>
<div>我是插槽的内容</div>
</saybyebye>
</div>
<template id="myhello">
<div>hello,{{name}}</div>
</template>
</body>
<script src="../js/vue2.7.js"></script>
<script>
// 注册了一个全局组件,名字叫hello
Vue.component('hello',{
template:`#myhello`,
data(){
return{
name:'我是hello'
}
}
})
// 定义一个局部组件
var saybyebye={
template:`
你好
// 插槽内容
`
}
var app = new Vue({
el:'#app',
data(){
return{
name:"张三",
}
},
// 注册局部组件
components:{
saybyebye
}
})
</script>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<div id="app">
大家好,{{name}}
<hello></hello>
<saybyebye>
<div slot="niu1">我是插槽的内容</div>
<template #niu2>
<div>你好niu2</div>
</template>
<template v-slot:niu3>
<div>你好niu3</div>
</template>
</saybyebye>
</div>
<template id="myhello">
<div>hello,{{name}}</div>
</template>
</body>
<script src="../js/vue2.7.js"></script>
<script>
// 注册了一个全局组件,名字叫hello
Vue.component('hello',{
template:`#myhello`,
data(){
return{
name:'我是hello'
}
}
})
// 定义一个局部组件
var saybyebye={
template:`
你好niu1
// 插槽内容
`
}
var app = new Vue({
el:'#app',
data(){
return{
name:"张三",
}
},
// 注册局部组件
components:{
saybyebye
}
})
</script>
</html>
这两个属性的作用是获取到子组件实例数组和父组件实例。
有了实例,就可以很方便的操作组件的属性和方法。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<div id="app">
大家好{{name}}
<button @click="ouda">打一顿</button>
<Myheader ref="dawa"></Myheader>
</div>
</body>
<script src="../js/vue2.7.js"></script>
<script>
Vue.prototype.$middleBus = new Vue();
var Myheader = {
template:`
子组件
{{xingming}}--{{Hp}}
`,
data(){
return{
xingming:'林宇豪',
Hp:100,
}
},
}
var app = new Vue({
el:'#app',
data(){
return{
name:'小豪',
}
},
methods:{
ouda(){
console.log("孽子,打一顿");
this.$refs.dawa.Hp = this.$refs.dawa.Hp - 10
},
},
components:{
Myheader
}
})
</script>
</html>
$refs的使用需要,在子元素上通过ref属性声明自己的引用名称
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<div id="app">
大家好,我是{{name}}
<Myheader ref="dawa"></Myheader>
<Myheadererwa ref="erwa" ></Myheadererwa>
</div>
</body>
<script src="../js/vue2.7.js"></script>
<script>
Vue.prototype.$middleBus = new Vue();
var Myheader = {
template:`
`,
}
var Myheadererwa = {
template:`
`,
data(){
return{
}
},
methods:{
jiao(){
this.$parent.name="爷爷"
},
},
}
var app = new Vue({
el:'#app',
data(){
return{
name:'小豪',
}
},
components:{
Myheader,
Myheadererwa
}
})
</script>
</html>
<Myheader ref="header" age="18" :sex="sex"></Myheader>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<div id="app">
大家好{{name}}
<button @click="ouda">打一顿</button>
<button @click="anwei">安慰</button>
<button @click="xiaodao">看看导哥在干嘛</button>
<Myheader ref="dawa"></Myheader>
<Myheadererwa ref="erwa" age="2" :nengli="nengli2"></Myheadererwa>
<div id="mydiv"></div>
</div>
</body>
<script src="../js/vue2.7.js"></script>
<script>
Vue.prototype.$middleBus = new Vue();
var Myheader = {
template:`
子组件
{{xingming}}--{{Hp}}
`,
data(){
return{
xingming:'林宇豪',
Hp:100,
}
},
}
var Myheadererwa = {
template:`
子组件
{{xingming}}--{{Hp}}
二娃 = {{age}} -- {{nengli}}
`,
data(){
return{
xingming:'王导',
Hp:0,
}
},
methods:{
see(){
console.log("再看岛国动作片");
},
jiao(){
this.$parent.name="爷爷"
},
},
mounted(){
this.$middleBus.$on('jieshou',val=>{
// 使用箭头函数,可以不改变this的指向,仍然和外部的this保持一致,指向child01
console.log(val);
});
},
props:['age','nengli'],
}
var app = new Vue({
el:'#app',
data(){
return{
name:'小豪',
nengli2:"千里眼,顺风耳"
}
},
methods:{
ouda(){
console.log("孽子,打一顿");
this.$refs.dawa.Hp = this.$refs.dawa.Hp - 10
// if(this.$refs.dawa.Hp<=0){
// document.getElementById("#mydiv").innerHTML="已经死了不能在死了爹"
// }
},
anwei(){
console.log("抽了一巴掌,安慰了一下");
this.$refs.erwa.Hp = this.$refs.erwa.Hp + 10
},
xiaodao(){
this.$refs.erwa.see()
},
},
components:{
Myheader,
Myheadererwa
}
})
</script>
</html>
<Myheadererwa -parent-event="bainian" ref="erwa" age="2" :nengli="nengli2"></Myheadererwa>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<div id="app">
大家好{{changname}}
<button @click="ouda">打一顿</button>
<button @click="anwei">安慰</button>
<button @click="xiaodao">看看导哥在干嘛</button>
<Myheader ref="dawa"></Myheader>
<Myheadererwa @to-parent-event="bainian" ref="erwa" age="2" :nengli="nengli2"></Myheadererwa>
<div id="mydiv"></div>
</div>
</body>
<script src="../js/vue2.7.js"></script>
<script>
Vue.prototype.$middleBus = new Vue();
var Myheader = {
template:`
子组件
{{xingming}}--{{Hp}}
`,
data(){
return{
xingming:'林宇豪',
Hp:100,
}
},
methods:{
chuanzhi(){
this.$middleBus.$emit('jieshou','你好child01,我是child02');
}
}
}
var Myheadererwa = {
template:`
子组件
{{xingming}}--{{Hp}}
二娃 = {{age}} -- {{nengli}}
`,
data(){
return{
xingming:'王导',
Hp:0,
}
},
methods:{
see(){
console.log("再看岛国动作片");
},
jiao(){
this.$parent.name="爷爷"
},
happyNewYear(){
// 触发自定义事件
this.$emit('to-parent-event',this.xingming)
}
},
mounted(){
this.$middleBus.$on('jieshou',val=>{
// 使用箭头函数,可以不改变this的指向,仍然和外部的this保持一致,指向child01
console.log(val);
});
},
props:['age','nengli'],
}
var app = new Vue({
el:'#app',
data(){
return{
changname:'小豪',
nengli2:"千里眼,顺风耳"
}
},
methods:{
ouda(){
console.log("孽子,打一顿");
this.$refs.dawa.Hp = this.$refs.dawa.Hp - 10
// if(this.$refs.dawa.Hp<=0){
// document.getElementById("#mydiv").innerHTML="已经死了不能在死了爹"
// }
},
anwei(){
console.log("抽了一巴掌,安慰了一下");
this.$refs.erwa.Hp = this.$refs.erwa.Hp + 10
},
xiaodao(){
this.$refs.erwa.see()
},
bainian(xingming){
console.log(xingming+"给您拜年了 ");
}
},
components:{
Myheader,
Myheadererwa
}
})
</script>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<div id="app">
大家好{{changname}}
<button @click="ouda">打一顿</button>
<button @click="anwei">安慰</button>
<button @click="xiaodao">看看导哥在干嘛</button>
<Myheader ref="dawa"></Myheader>
<Myheadererwa @to-parent-event="bainian" ref="erwa" age="2" :nengli="nengli2"></Myheadererwa>
<div id="mydiv"></div>
</div>
</body>
<script src="../js/vue2.7.js"></script>
<script>
Vue.prototype.$middleBus = new Vue();
var Myheader = {
template:`
子组件
{{xingming}}--{{Hp}}
`,
data(){
return{
xingming:'林宇豪',
Hp:100,
}
},
methods:{
chuanzhi(){
this.$middleBus.$emit('jieshou','你好child01,我是child02');
}
}
}
var Myheadererwa = {
template:`
子组件
{{xingming}}--{{Hp}}
二娃 = {{age}} -- {{nengli}}
`,
data(){
return{
xingming:'王导',
Hp:0,
}
},
methods:{
see(){
console.log("再看岛国动作片");
},
jiao(){
this.$parent.name="爷爷"
},
happyNewYear(){
// 触发自定义事件
this.$emit('to-parent-event',this.xingming)
}
},
mounted(){
this.$middleBus.$on('jieshou',val=>{
// 使用箭头函数,可以不改变this的指向,仍然和外部的this保持一致,指向child01
console.log(val);
});
},
props:['age','nengli'],
}
var app = new Vue({
el:'#app',
data(){
return{
changname:'小豪',
nengli2:"千里眼,顺风耳"
}
},
methods:{
ouda(){
console.log("孽子,打一顿");
this.$refs.dawa.Hp = this.$refs.dawa.Hp - 10
// if(this.$refs.dawa.Hp<=0){
// document.getElementById("#mydiv").innerHTML="已经死了不能在死了爹"
// }
},
anwei(){
console.log("抽了一巴掌,安慰了一下");
this.$refs.erwa.Hp = this.$refs.erwa.Hp + 10
},
xiaodao(){
this.$refs.erwa.see()
},
bainian(xingming){
console.log(xingming+"给您拜年了 ");
}
},
components:{
Myheader,
Myheadererwa
}
})
</script>
</html>
Vue.prototype.$middleBus = new Vue();
this.$middleBus.$emit('sendMsg','你好child01,我是child02');
this.$middleBus.$on('sendMsg',val=>{
// 使用箭头函数,可以不改变this的指向,仍然和外部的this保持一致,指向child01
this.msg = val;
});
定义
写法
var myMixin = {
data() {
return {
mixinname: '混入姓名',
};
},
mounted() {
console.log('我是混入的组件');
},
};
mixins: [myMixin],
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<div id="app">
大家好{{changname}}
<button @click="ouda">打一顿</button>
<button @click="anwei">安慰</button>
<button @click="xiaodao">看看导哥在干嘛</button>
<Myheader ref="dawa"></Myheader>
<Myheadererwa @to-parent-event="bainian" ref="erwa" age="2" :nengli="nengli2"></Myheadererwa>
<div id="mydiv"></div>
</div>
</body>
<script src="../js/vue2.7.js"></script>
<script>
Vue.prototype.$middleBus = new Vue();
var commonMixin = {
data(){
return{
mixinName:'葫芦'
}
},
mounted(){
console.log( "混入对象" +this.mixinName);
}
}
var Myheader = {
template:`
子组件
{{xingming}}--{{Hp}}---{{mixinName}}
`,
data(){
return{
xingming:'林宇豪',
Hp:100,
}
},
methods:{
chuanzhi(){
this.$middleBus.$emit('jieshou','你好child01,我是child02');
}
},
mixins:[commonMixin]
}
var Myheadererwa = {
template:`
子组件
{{xingming}}--{{Hp}}
二娃 = {{age}} -- {{nengli}}
`,
data(){
return{
xingming:'王导',
Hp:0,
}
},
methods:{
see(){
console.log("再看岛国动作片");
},
jiao(){
this.$parent.name="爷爷"
},
happyNewYear(){
// 触发自定义事件
this.$emit('to-parent-event',this.xingming)
}
},
mounted(){
this.$middleBus.$on('jieshou',val=>{
// 使用箭头函数,可以不改变this的指向,仍然和外部的this保持一致,指向child01
console.log(val);
});
},
props:['age','nengli'],
}
var app = new Vue({
el:'#app',
data(){
return{
changname:'小豪',
nengli2:"千里眼,顺风耳"
}
},
methods:{
ouda(){
console.log("孽子,打一顿");
this.$refs.dawa.Hp = this.$refs.dawa.Hp - 10
// if(this.$refs.dawa.Hp<=0){
// document.getElementById("#mydiv").innerHTML="已经死了不能在死了爹"
// }
},
anwei(){
console.log("抽了一巴掌,安慰了一下");
this.$refs.erwa.Hp = this.$refs.erwa.Hp + 10
},
xiaodao(){
this.$refs.erwa.see()
},
bainian(xingming){
console.log(xingming+"给您拜年了 ");
}
},
components:{
Myheader,
Myheadererwa
}
})
</script>
</html>
全局混入
Vue.mixin(myMixin);
注意
祝大家: 愿每个人都能遵循自己的时钟,做不后悔的选择。