Vue 组件注册全解析


Posted in Vue.js onDecember 17, 2020

全局组件注册语法

components中的两个参数组件名称和组件内容

Vue.component(组件名称, {
   data: 组件数据,
   template:组件模板内容
  })

全局组件注册应用

组件创建:

Vue.component('button-counter', {
   data: function(){
    return {
     count: 0
    }
   },
   template: '<button @click="handle">点击了{{count}}次</button>',
   methods: {
    handle: function(){
     this.count ++;
    }
   }
  })
  var vm = new Vue({
   el: '#app',
   data: {
   }
  });

如何在页面中运用,直接在页面中应用组件名称

<div id="app">
  <button-counter></button-counter>
</div>

这个组件是可以重用的,直接在页面中多次使用,切数据相互独立,互不干扰

组件注册注意事项

1.data必须是一个函数

  • 分析函数与普通对象的对比

2.组件模板内容必须是单个根元素

  • 分析演示实际的效果

3.组件模板内容可以是模板字符串

  • 模板字符串需要浏览器提供支持(ES6语法)

4.组件命名方式

  • 短横线方式
Vue.component('my-component',{/*...*/})

驼峰方式

Vue.component('MyComponent',{/*...*/})

如果使用驼峰式命名组件,那么在使用组件的时候,只能在字符串模板中用驼峰的方式使用组件,但是在普通的标签模板中,必须使用短横线的方式使用组件

局部组件

局部组件只能在注册它的父组件中使用

局部组件注册语法

var ComponentA = {/*...*/}
var ComponentB = {/*...*/}
var ComponentC = {/*...*/}
new Vue({
  el : '#app',
  components: {

'component-a' : ComponentA,


'component-b' : ComponentB,


'component-c' : ComponentC

}
})

组件的创建

Vue.component('test-com',{
   template: '<div>Test<hello-world></hello-world></div>'
  });
  var HelloWorld = {
   data: function(){
    return {
     msg: 'HelloWorld'
    }
   },
   template: '<div>{{msg}}</div>'
  };
  var HelloTom = {
   data: function(){
    return {
     msg: 'HelloTom'
    }
   },
   template: '<div>{{msg}}</div>'
  };
  var HelloJerry = {
   data: function(){
    return {
     msg: 'HelloJerry'
    }
   },
   template: '<div>{{msg}}</div>'
  };
  var vm = new Vue({
   el: '#app',
   data: {
    
   },
   components: {
    'hello-world': HelloWorld,
    'hello-tom': HelloTom,
    'hello-jerry': HelloJerry
   }
  });

页面中的应用

<div id="app">
  <hello-world></hello-world>
  <hello-tom></hello-tom>
  <hello-jerry></hello-jerry>
  <test-com></test-com>
 </div>

以上就是Vue 组件注册全解析的详细内容,更多关于Vue 组件注册的资料请关注三水点靠木其它相关文章!

Vue.js 相关文章推荐
vue+iview实现分页及查询功能
Nov 17 Vue.js
如何在vue中使用kindeditor富文本编辑器
Dec 19 Vue.js
vue+openlayers绘制省市边界线
Dec 24 Vue.js
vue 页面跳转的实现方式
Jan 12 Vue.js
vue使用过滤器格式化日期
Jan 20 Vue.js
vue.js实现点击图标放大离开时缩小的代码
Jan 27 Vue.js
Vue实现跑马灯样式文字横向滚动
Nov 23 Vue.js
vue实力踩坑之push当前页无效
Apr 10 Vue.js
如何优化vue打包文件过大
Apr 13 Vue.js
Vue操作Storage本地化存储
Apr 29 Vue.js
vue本地构建热更新卡顿的问题“75 advanced module optimization”完美解决方案
Aug 05 Vue.js
Vue router配置与使用分析讲解
Dec 24 Vue.js
vue图片裁剪插件vue-cropper使用方法详解
Dec 16 #Vue.js
vue实现图片裁剪后上传
Dec 16 #Vue.js
Vue-router中hash模式与history模式的区别详解
Dec 15 #Vue.js
vue项目中企业微信使用js-sdk时config和agentConfig配置方式详解
Dec 15 #Vue.js
Vue解决移动端弹窗滚动穿透问题
Dec 15 #Vue.js
8个非常实用的Vue自定义指令
Dec 15 #Vue.js
vue从后台渲染文章列表以及根据id跳转文章详情详解
Dec 14 #Vue.js
You might like
用PHP 快速生成 Flash 动画的方法
2007/03/06 PHP
PHP Mysql编程之高级技巧
2008/08/27 PHP
PHP中文分词的简单实现代码分享
2011/07/17 PHP
PHPExcel内存泄漏问题解决方法
2015/01/23 PHP
php+mysql查询实现无限下级分类树输出示例
2016/10/03 PHP
解密效果
2006/06/23 Javascript
Javascript this指针
2009/07/30 Javascript
读jQuery之十一 添加事件核心方法
2011/07/31 Javascript
js中style.display=&quot;&quot;无效的解决方法
2014/10/30 Javascript
Jquery ajax 同步阻塞引起的UI线程阻塞问题
2015/11/17 Javascript
JS实用的带停顿的逐行文本循环滚动效果实例
2016/11/23 Javascript
Bootstrap源码解读网格系统(3)
2016/12/22 Javascript
利用jQuery解析获取JSON数据
2017/04/08 jQuery
Angularjs Promise实例详解
2018/03/15 Javascript
JS深入学习之数组对象排序操作示例
2020/05/01 Javascript
js实现弹幕飞机效果
2020/08/27 Javascript
python网络编程学习笔记(一)
2014/06/09 Python
Python循环语句之break与continue的用法
2015/10/14 Python
Python cookbook(字符串与文本)针对任意多的分隔符拆分字符串操作示例
2018/04/19 Python
win7+Python3.5下scrapy的安装方法
2018/07/31 Python
virtualenv 指定 python 解释器的版本方法
2018/10/25 Python
Python图像滤波处理操作示例【基于ImageFilter类】
2019/01/03 Python
python字典嵌套字典的情况下找到某个key的value详解
2019/07/10 Python
基于CSS3实现的黑色个性导航菜单效果
2015/09/14 HTML / CSS
浅析HTML5 Landmark
2020/09/11 HTML / CSS
英国最大的手表网站:The Watch Hut
2017/03/31 全球购物
FC-Moto英国:欧洲最大的摩托车服装和头盔商店之一
2019/08/25 全球购物
宾馆总经理岗位职责
2014/02/14 职场文书
个人收入证明模板
2014/09/18 职场文书
租车协议书范本2014
2014/11/17 职场文书
店铺转让协议书
2014/12/02 职场文书
公司优秀员工推荐信
2015/03/24 职场文书
实施意见格式范本
2015/06/05 职场文书
公司车队管理制度
2015/08/04 职场文书
收音机爱好者玩机13年,简评其使用过的19台收音机
2022/04/30 无线电
Windows Server 2022 超融合部署(图文教程)
2022/06/25 Servers