一 组件基础
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:`
<button @click="count++">You clicked me {{ count }} times.</button>
`,
data(){
return {
count:0
}
}
})
let vm = new Vue({
el: '#app',
data: {},
})
</script>
</html>
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: `
<div>
<button @click="handleClick">返回</button>
我是NavBar
<button style="background: red">主页</button>
<br>
<child></child>
</div>
`,
methods: {
handleClick() {
console.log('nav nav')
},
},
components: {
child: {
template: `<button>navbar的子标签</button>`,
}
}
})
let vm = new Vue({
el: '#app',
data: {}
})
</script>
</html>
二 组件通信
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: `
<div>
<h1>我是init组件</h1>
<input type="text" v-model="name"> ---》{{ name }}
<br>
<button @click="handleSend">点我把name传给父组件</button>
</div>`,
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>
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: `
<div>
<button>返回</button>
父组件传递的内容是:{{myname}}
传入的布尔是{{isshow}}
<button>主页</button>
<br>
</div>
`,
props:{
myname:String,
isshow:Boolean,
},
})
var vm = new Vue({
el: '#app',
data: {
name:'jasper'
},
})
</script>
</html>
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:`<div>child</div>`,
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>
三 动态组件
<!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: `<div>
首页
</div>`,
})
Vue.component('child2', {
template: `<div>
商品
<input type="text">
</div>`,
})
Vue.component('child3', {
template: `<div>
购物车
</div>`,
})
var vm = new Vue({
el: '#box',
data: {
who:'child1'
},
})
</script>
</html>