组件的注册
-
通过实例化Vue构造器函数得到一个Vue实例,这个实例我们称之为’根实例’,它是最大的父级
-
这个根实例是以标签的形式存在的,那么我们也称之为’ 根组件 ’
-
根实例也是一个组件,但是我们得到只是跟组件
-
Vue.extend() 它就是对Vue功能的扩展,这个扩展就是组件
-
Vue是通过 Vue.extend() 来实现【 扩展 】 Vue的功能的,这个功能就是组件
-
思考: Vue.extend如何使用?
- 通过new Vue.extend() 发现和new Vue一样了 排除了
- 组件就是一个以标签化呈现的东西,所以我应该像标签一样使用
- 但是无论是 html3 还是 html5 肯定不会同意它随意来个标签的
- Vue会将组件编译成html结构
- Vue的这个处理过程,我们称之为 ’ 组件注册 ’
总结:
1. Vue是通过Vue.extend() 来实现组件的
2. Vue的组件的使用时需要注册的
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<div id="app">
<!-- 不能这么写 -->
<!-- <HelloBoy></HelloBoy> -->
<!-- <hello-boy></hello-boy> -->
<Hello></Hello>
<hello></hello>
</div>
<div id="root">
<Hello></Hello>
</div>
</body>
<script src="../../lib/vue.js"></script>
<script>
/*
全局注册
1. 格式: Vue.component()
*/
// const Hello = Vue.extend( options ) 我们之所学的选项这里都可以用
const Hello = Vue.extend({
template: '<div> Hello 组件 </div>'
})
// Vue.component( 组件的名称,组件的配置 )
// Vue.component( 'HelloBoy', Hello )
// Vue.component( 'hello-boy', Hello )
Vue.component( 'Hello', Hello )
new Vue({
el: '#app'
})
new Vue({
el: '#root'
})
</script>
</html>
简写
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<div id="app">
<!-- 不能这么写 -->
<!-- <HelloBoy></HelloBoy> -->
<!-- <hello-boy></hello-boy> -->
<Hello></Hello>
<hello></hello>
</div>
<div id="root">
<Hello></Hello>
</div>
</body>
<script src="../../lib/vue.js"></script>
<script>
/*
全局注册
1. 格式: Vue.component()
*/
// vue组件注册的简写
Vue.component( 'Hello', {
template: '<div> Hello 组件 </div>'
})
new Vue({
el: '#app'
})
new Vue({
el: '#root'
})
</script>
</html>
组件的局部注册
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<div id="app">
<!-- 不能这么写 -->
<!-- <HelloBoy></HelloBoy> -->
<!-- <hello-boy></hello-boy> -->
<Hello></Hello>
<hello></hello>
</div>
<div id="root">
<Hello></Hello>
</div>
</body>
<script src="../../lib/vue.js"></script>
<script>
/*
局部注册使用 components选线来完成
局部注册只在当前注册的实例范围内有效
*/
new Vue({
el: '#app',
components: { //局部注册组件的选项
// 组件的名称: 组件的选项
'Hello': {
template: '<div> Hello 这里是局部注册 </div>'
}
}
})
new Vue({
el: '#root'
})
</script>
</html>
组件的使用规则
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<div id="app">
<table border="1">
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
</tr>
<tr is = "Hello"></tr>
</table>
</div>
</body>
<script src="../../lib/vue.js"></script>
<script>
/*
业务: 给上面的表格增加一个行,这一行用一个组件表示
通过以上案例发现: 组件在父子级是有直接关系的标签中是不能直接解析的,会出问题
直接父子级关系的标签
ul li
ol li
table tr td
dl dd dt
select
如何解决这个问题?
我们使用is属性来解决
*/
Vue.component('Hello',{
template: ` <tr>
<td>4</td>
<td>5</td>
<td>6</td>
</tr>`
})
new Vue({
el: '#app'
})
</script>
</html>