1 虚拟dom_diff
2 定义组件
1)
2)
先写一个最简单的全局组件
需要注意 几件事情
1、 注意写的顺序 必须是Vue.component 定义在前面 new Vue({})定义在后面
2、如果全局组件定义的是 navbar 上面也必须用这个 不过渲染后变成了 div
3、全局组件中 使用方法 直接写在 和template 同级别就好 不能写在 new Vue中
<html>
<head>
<title>test</title>
<meta charset="utf-8" />
<style></style>
</head>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<body>
<div id="box">
<navbar></navbar>
</div>
</body>
<script>
Vue.component('navbar', {
template: `
<div>
<button @click="getClick()">返回</button>
navbar
<button>主页</button>
</div>
`,
methods: {
getClick() {
console.log('back');
}
}
}),
new Vue({
el: "#box"
})
</script>
</html>
3)局部组件
有几个需要注意的事情
1、 new Vue({}) 它在定义的时候 同时生成一个 根组件 root
2、它里面定义的局部 组件 components:{} 只能在它内部使用
3、全局定义组件 Vue.component:{} 可以在任何地方使用
<html>
<head>
<title>test</title>
<meta charset="utf-8" />
<style></style>
</head>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<body>
<div id="box">
<navbar></navbar>
<navchild></navchild>
</div>
</body>
<script>
Vue.component('navbar', {
template: `
<div>
<button @click="getClick()">返回</button>
navbar
<button>主页</button>
</div>
`,
methods: {
getClick() {
console.log('back');
}
}
}),
new Vue({
el: "#box",
components: {
navchild: {
template: `<div>navchild</div>`,
}
}
})
</script>
</html>
4)组件和实例区别
5)父传子
属性向下传递
有几个事情需要先说明一下
1、new Vue({}) 创建 会产生一个根节点 是所有节点的父节点
2、 父节点的数据和属性可以传给子节点
3、一个节点如果在另一个组件内使用 就作为它的子节点
4、跟节点传递数据 需要利用 v-bind 绑定
<html>
<head>
<title>test</title>
<meta charset="utf-8" />
<style></style>
</head>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<body>
<div id="box">
<navbar myname="home"></navbar>
<navbar myname="list"></navbar>
<navbar :myname="parent"></navbar>
</div>
</body>
<script>
Vue.component("navbar", {
template: `
<div>
<button>返回</button>
navbar--{{myname}}
<button>首页</button>
</div>`,
props: ["myname"],
}
)
new Vue({
el: "#box",
data: {
"parent": '根组件的状态',
}
})
</script>
</html>
需要注意另外一个问题,如果 加上另外一个控制是否显示一些东西的情况
加一点 属性验证
6) 子传父
事件向上传递
<html>
<head>
<title>test</title>
<meta charset="utf-8" />
<style></style>
</head>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<body>
<div id="box">
<child @myevent="handleEvent"></child>
</div>
</body>
<script>
// 子组件
Vue.component('child', {
template: `<div>
child组件
<button @click="payMoney"> click</button>
</div>`,
methods: {
payMoney(ev) {
this.$emit('myevent', 1000) //分发事件给父组件
}
}
})
new Vue({
el: "#box",
methods: {
handleEvent(ev) {
console.log("父组件收到钱了", ev)
}
}
})
</script>
</html>
7) 子传父 找个案例 加深理解
点击子组件内的按钮可以实现 显示和隐藏
怎么做 ?
1)
这个实现非常的简单 但是如果现在的操作按钮在子组件里面呢 ?
<html>
<head>
<title>test</title>
<meta charset="utf-8" />
<style></style>
</head>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<body>
<!-- 父组件 -->
<div id="box">
<button @click="isShow=!isShow">click</button>
<sidebar v-show="isShow"></sidebar>
</div>
</body>
<script>
Vue.component('sidebar', {
template: ` <div style="background:yellow;width:200px">
<ul>
<li>111</li>
<li>222</li>
<li>333</li>
</ul>
</div>`
})
new Vue({
el: "#box",
data: {
isShow: false,
}
})
</script>
</html>
2)
如果当前的操作按钮在子组件中,我们应该怎么处理这个事情 ?
有另外 一个按钮开始定义在 另一个子组件 navbar中
<html>
<head>
<title>test</title>
<meta charset="utf-8" />
<style></style>
</head>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<body>
<!-- 父组件 -->
<div id="box">
<navbar @event="handleEvent"></navbar>
<!-- <button @click="isShow=!isShow">click</button> -->
<sidebar v-show="isShow"></sidebar>
</div>
</body>
<script>
// 另外一个子组件 navbar
Vue.component('navbar', {
template: `
<div>
navbar
<button @click="handleClick">click</button>
</div>
`,
methods: {
// 子组件点击之后 将事件 event(随便写的)上抛给父组件 父组件处理
handleClick() {
this.$emit('event')
}
}
})
// 一个子组件 sidebar
Vue.component('sidebar', {
template: ` <div style="background:yellow;width:200px">
<ul>
<li>111</li>
<li>222</li>
<li>333</li>
</ul>
</div>`
})
// 这个是父组件 含有一个根组件
new Vue({
el: "#box",
data: {
isShow: false,
},
methods: {
handleEvent() {
this.isShow = !this.isShow
}
}
})
</script>
</html>