一 组件基础
1.1 定义全局组件
DOCTYPE html >
< html lang = " en" >
< head>
< meta charset = " UTF-8" >
< title> Title title>
< script src = " vue.js" > script>
head>
< body>
< div id = " app" >
< button-counter> button-counter>
< button-counter> button-counter>
< button-counter> button-counter>
div>
body>
< script>
Vue. component ( 'button-counter' , {
template : `
You clicked me {{ count }} times.
` ,
data ( ) {
return {
count : 0
}
}
} )
let vm = new Vue ( {
el : '#app' ,
data : { } ,
} )
script>
html>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
1.2 定义局部组件
DOCTYPE html >
< html lang = " en" >
< head>
< meta charset = " UTF-8" >
< title> Title title>
< script src = " vue.js" > script>
head>
< body>
< div id = " app" >
< navbar> navbar>
div>
body>
< script>
Vue. component ( 'navbar' , {
template : `
返回
我是NavBar
主页
` ,
methods : {
handleClick ( ) {
console. log ( 'nav nav' )
} ,
} ,
components : {
child : {
template : ` navbar的子标签 ` ,
}
}
} )
let vm = new Vue ( {
el : '#app' ,
data : { }
} )
script>
html>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
二 组件通信
2.1 父子通信之子传父
DOCTYPE html >
< html lang = " en" >
< head>
< meta charset = " UTF-8" >
< title> Title title>
< script src = " vue.js" > script>
head>
< body>
< div id = " app" >
< h1> 自定义事件实现父子通信之子传父 h1>
父组件中的name值为:{{name}}
< hr>
< init @myevnet = " handleEvent" > init>
< hr>
div>
body>
< script>
var init = {
template : `
我是init组件
---》{{ name }}
点我把name传给父组件
` ,
data ( ) {
return {
name : ''
}
} ,
methods : {
handleSend ( ) {
this . $emit ( 'myevnet' , this . name)
}
}
}
var vm = new Vue ( {
el : '#app' ,
data : {
name : '刘亦菲'
} ,
methods : {
handleEvent ( name ) {
this . name = name
}
} ,
components : {
init
}
} )
script>
html>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
2.2 父子通信之父传子
DOCTYPE html >
< html lang = " en" >
< head>
< meta charset = " UTF-8" >
< title> Title title>
< script src = " vue.js" > script>
head>
< body>
< div id = " app" >
< navbar :myname = " name" :isshow = " true" > navbar>
div>
body>
< script>
Vue. component ( 'navbar' , {
template : `
返回
父组件传递的内容是:{{myname}}
传入的布尔是{{isshow}}
主页
` ,
props : {
myname : String,
isshow : Boolean,
} ,
} )
var vm = new Vue ( {
el : '#app' ,
data : {
name : 'jasper'
} ,
} )
script>
html>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
2.3 ref属性
DOCTYPE html >
< html lang = " en" >
< head>
< meta charset = " UTF-8" >
< title> Title title>
< script src = " vue.js" > script>
head>
< body>
< div id = " app" >
< input type = " text" ref = " mytext" >
< button @click = " handleClick" > 点我 button>
< child ref = " mychild" > child>
div>
body>
< script>
Vue. component ( 'child' , {
template : ` child
` ,
data ( ) {
return {
text : '子组件数据'
}
} ,
methods : {
add ( ) {
console. log ( '子组件的add方法' )
}
}
} )
var vm = new Vue ( {
el : '#app' ,
data : {
} ,
methods : {
handleClick ( ) {
console. log ( this )
console. log ( this . $refs. mytext. value)
console. log ( this . $refs. mychild. text)
this . $refs. mychild. add ( '传递参数' )
}
}
} )
script>
html>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
三 动态组件
DOCTYPE html >
< html lang = " en" >
< head>
< meta charset = " UTF-8" >
< title> Title title>
< script src = " vue.js" > script>
head>
< body>
< div id = " box" >
< ul>
< li> < a @click = " who=' child1' " > 首页 a> li>
< li> < a @click = " who=' child2' " > 商品 a> li>
< li> < a @click = " who=' child3' " > 购物车 a> li>
ul>
< keep-alive>
< component :is = " who" > component>
keep-alive>
div>
body>
< script>
Vue. component ( 'child1' , {
template : `
首页
` ,
} )
Vue. component ( 'child2' , {
template : `
商品
` ,
} )
Vue. component ( 'child3' , {
template : `
购物车
` ,
} )
var vm = new Vue ( {
el : '#box' ,
data : {
who : 'child1'
} ,
} )
script>
html>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47