vue组件component的使用
什么是组件化与模块化的区别?
- 什么是组件:组件的出现,就是为了拆分Vue实例的代码量,能够让我们以不同的组件,来划分不同的功能模块
,将来我们需要什么功能,就可以去调用对应的组件即可
- 组件化与模块化的不同:
模块化:是从代码逻辑的角度进行分析,方便代码分层开发,保证每个功能模块的只能单一
组件化:是从UI界面的角度进行划分,前端的组件化,方便UI组件的重用。
组件使用
自己常用的如下:
创建组件
components>nav文件
![](http://upload-images.jianshu.io/upload_images/16913231-65bd7783687b9c12.png?imageMogr2/auto-orient/strip|imageView2/2/w/990/format/webp)
image.png
使用组件
1 引入组件 \ 创建私有组件 \ 使用
![](http://upload-images.jianshu.io/upload_images/16913231-79a7f55fe2ca1145.png?imageMogr2/auto-orient/strip|imageView2/2/w/618/format/webp)
image.png
展示
![](http://upload-images.jianshu.io/upload_images/16913231-853d53535e761765.png?imageMogr2/auto-orient/strip|imageView2/2/w/1200/format/webp)
image.png
<!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> <script src="./lib/vue-2.4.0.js"></script> </head> <body>
<span class="token tag"><span class="token tag"><span class="token punctuation"><</span>div</span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>app<span class="token punctuation">"</span></span><span class="token punctuation">></span></span> <span class="token comment"><!-- 引用组件的名称 --></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>login</span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation"></</span>login</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>login2</span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation"></</span>login2</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>login3</span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation"></</span>login3</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>hr</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>login6</span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation"></</span>login6</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"></</span>div</span><span class="token punctuation">></span></span> <span class="token comment"><!-- 注意 这里的id是必须的,不要在实例控制中的divcalss app 中去写template --></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>template</span> <span class="token attr-name">id</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>login3<span class="token punctuation">"</span></span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>div</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>h2</span><span class="token punctuation">></span></span>我是第三种方式创建出来的<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>h2</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"></</span>div</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"></</span>template</span><span class="token punctuation">></span></span>
<!-- Vue实例 -->
<script>
// Component 组件的创建
// 注意 template 中的容器 只能有一个父元素,不能包含两个 你可以用一个div包裹
// 第一种创建全局组件
// extend是注册组件函数,他返回一个对象
var comobj= Vue.extend({
template:"<h1>我是全局组件</h1>"
})
// component函数提交组件,第一个参数为组件的名称,第二个参数是一个注册组件的对象
Vue.component("login",comobj)// 第二种创建方式 // component 中直接写上一个模板对象 Vue.component('login2',{template:'<span class="token tag"><span class="token tag"><span class="token punctuation"><</span>h1</span><span class="token punctuation">></span></span>我是第二种v创建出来的组件<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>h1</span><span class="token punctuation">></span></span>'}); // 第三种创建的方式 首先在元素中创建一个template模板 Vue.component('login3',{template:'#login3'}) var vm=new Vue({ el:'.app', data:{}, // 定义私有组件 和全局差不多 组件名称和对象,也可以使用template模板来创建 components:{ login6:{template:'#login3'} } }) <span class="token tag"><span class="token tag"><span class="token punctuation"></</span>script</span><span class="token punctuation">></span></span>
</body>
</html>
- 组件的引用可以使用组件的名字直接引用 也可以使用Vue给我们提供的的元素
- Vue 提供了component,来展示对应名称的组件
- component是一个占位符,:is属性可以用来指定要展示的组件名称
- <compnent :is="comName" ></component>
<div class="app">
<login></login>
<login2></login2>
<!-- 使用component 占位符来展示组件 -->
<!-- 注意 :is 是绑定的属性,需要在实例的data中绑定的 组件的名称是字符串 -->
<!-- 其中 他还提供了 mode属性来切换动画的先进先出的问题 -->
<component :is="login='login'" mode="out-in"></component>
</div>
私有组件使用
简写
components:{ // 注册私有组件
mycom1, // 简写的方式
},
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <!-- 导入vue.js库 --> <script src="lib/vue.js"></script>
<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>head</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>body</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>div</span> <span class="token attr-name">id</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>app1<span class="token punctuation">"</span></span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>h1</span><span class="token punctuation">></span></span>{{ msg }}<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>h1</span><span class="token punctuation">></span></span> <span class="token comment"><!-- 引用私有组件my-com1 --></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>my-com1</span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation"></</span>my-com1</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"></</span>div</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>div</span> <span class="token attr-name">id</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>app2<span class="token punctuation">"</span></span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>h1</span><span class="token punctuation">></span></span>{{ msg }}<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>h1</span><span class="token punctuation">></span></span> <span class="token comment"><!-- 引用私有组件my-com1 --></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>my-com1</span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation"></</span>my-com1</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"></</span>div</span><span class="token punctuation">></span></span> <span class="token comment"><!-- 创建组件内容 --></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>template</span> <span class="token attr-name">id</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>tpl1<span class="token punctuation">"</span></span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>h2</span><span class="token punctuation">></span></span>创建vm1实例的私有组件<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>h2</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"></</span>template</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>script</span><span class="token punctuation">></span></span> // 创建第一个Vue的实例 var vm1 = new Vue({ el: '#app1', data: {"msg": "这是vm1实例"}, components:{ // 注册私有组件 "my-com1": { template: "#tpl1", } }, }) // 创建第二个Vue的实例 var vm2 = new Vue({ el: '#app2', data: {"msg": "这是vm2实例"}, components:{ // 注册私有组件 "my-com1": { template: "#tpl1", } }, }) <span class="token tag"><span class="token tag"><span class="token punctuation"></</span>script</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"></</span>body</span><span class="token punctuation">></span></span>
</html>