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 相关文章推荐
element-plus一个vue3.xUI框架(element-ui的3.x 版初体验)
Dec 02 Vue.js
vue使用element-ui实现表单验证
Dec 13 Vue.js
Vue如何跨组件传递Slot的实现
Dec 14 Vue.js
vue 通过base64实现图片下载功能
Dec 19 Vue.js
如何使用RoughViz可视化Vue.js中的草绘图表
Jan 30 Vue.js
Vue SPA 首屏优化方案
Feb 26 Vue.js
vue-router懒加载的3种方式汇总
Feb 28 Vue.js
vue前端工程的搭建
Mar 31 Vue.js
Vue如何实现组件间通信
May 15 Vue.js
vue+spring boot实现校验码功能
May 27 Vue.js
Vue的过滤器你真了解吗
Feb 24 Vue.js
vue 自定义组件添加原生事件
Apr 21 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中,文件上传
2006/12/06 PHP
PHP的伪随机数与真随机数详解
2015/05/27 PHP
javascript入门·动态的时钟,显示完整的一些方法,新年倒计时
2007/10/01 Javascript
window.location.hash 属性使用说明
2010/03/20 Javascript
jQuery 表格工具集
2010/04/25 Javascript
javascript 设为首页与加入收藏兼容多浏览器代码
2011/01/11 Javascript
js 剪切板的用法(clipboardData.setData)与js match函数介绍
2013/11/19 Javascript
jquery动态添加option示例
2013/12/30 Javascript
js如何改变文章的字体大小
2016/01/08 Javascript
AngularJS入门教程之多视图切换用法示例
2016/11/02 Javascript
JS中的phototype详解
2017/02/04 Javascript
vue 项目常用加载器及配置详解
2018/01/22 Javascript
Vue实现点击后文字变色切换方法
2018/02/11 Javascript
vue实现提示保存后退出的方法
2018/03/15 Javascript
AngularJS模态框模板ngDialog的使用详解
2018/05/11 Javascript
在Koa.js中实现文件上传的接口功能
2019/10/08 Javascript
ES6 Promise对象概念及用法实例详解
2019/10/15 Javascript
[44:58]2018DOTA2亚洲邀请赛 4.5 淘汰赛 LGD vs Liquid 第二场
2018/04/06 DOTA
Python EOL while scanning string literal问题解决方法
2020/09/18 Python
python自定义异常实例详解
2017/07/11 Python
python中的二维列表实例详解
2018/06/19 Python
python去除拼音声调字母,替换为字母的方法
2018/11/28 Python
Python动态参数/命名空间/函数嵌套/global和nonlocal
2019/05/29 Python
python实现动态创建类的方法分析
2019/06/25 Python
详解appium自动化测试工具(monitor、uiautomatorviewer)
2021/01/27 Python
Python抖音快手代码舞(字符舞)的实现方法
2021/02/07 Python
HTML5 在canvas中绘制文本附效果图
2014/06/23 HTML / CSS
美国女性奢华品牌精品店:INTERMIX
2017/10/12 全球购物
校园招聘策划书
2014/01/09 职场文书
集体婚礼证婚词
2014/01/13 职场文书
青春无悔演讲稿
2014/05/08 职场文书
相亲活动方案
2014/08/26 职场文书
SpringRetry重试框架的具体使用
2021/07/25 Java/Android
详解jQuery的核心函数和事件处理
2022/02/18 jQuery
Ruby使用Mysql2连接操作MySQL
2022/04/19 Ruby
MySQL数据库实验之 触发器和存储过程
2022/06/21 MySQL