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项目如何引入bootstrap、elementUI、echarts
Nov 26 Vue.js
在Vue中使用CSS3实现内容无缝滚动的示例代码
Nov 27 Vue.js
vue3.0中setup使用(两种用法)
Dec 02 Vue.js
vue-calendar-component 封装多日期选择组件的实例代码
Dec 04 Vue.js
vue祖孙组件之间的数据传递案例
Dec 07 Vue.js
Vue 实现一个简单的鼠标拖拽滚动效果插件
Dec 10 Vue.js
vue实现图片裁剪后上传
Dec 16 Vue.js
详解Vue2的diff算法
Jan 06 Vue.js
vue引入Excel表格插件的方法
Apr 28 Vue.js
详解Vue中$props、$attrs和$listeners的使用方法
Feb 18 Vue.js
教你部署vue项目到docker
Apr 05 Vue.js
VUE递归树形实现多级列表
Jul 15 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
Smarty结合Ajax实现无刷新留言本实例
2007/01/02 PHP
创建配置文件 用PHP写出自己的BLOG系统 2
2010/04/12 PHP
PHP处理JSON字符串key缺少双引号的解决方法
2014/09/16 PHP
Jquery的基本对象转换和文档加载用法实例
2015/02/25 Javascript
AngularJs实现ng1.3+表单验证
2015/12/10 Javascript
深入理解jQuery之防止冒泡事件
2016/05/24 Javascript
js实现简易垂直滚动条
2017/02/22 Javascript
BootStrap+Mybatis框架下实现表单提交数据重复验证
2017/03/23 Javascript
利用PM2部署node.js项目的方法教程
2017/05/10 Javascript
浅谈JS中的反柯里化( uncurrying)
2017/08/17 Javascript
jQuery实现IE输入框完成placeholder标签功能的方法
2017/09/20 jQuery
Vue.JS项目中5个经典Vuex插件
2017/11/28 Javascript
微信小程序解析富文本过程详解
2019/07/13 Javascript
基于layui轮播图满屏是高度自适应的解决方法
2019/09/16 Javascript
angular组件间传值测试的方法详解
2020/05/07 Javascript
Paypal支付不完全指北
2020/06/04 Javascript
vue实现顶部菜单栏
2020/11/08 Javascript
angular8.5集成TinyMce5的使用和详细配置(推荐)
2020/11/16 Javascript
[02:40]2014DOTA2 国际邀请赛中国区预选赛 四大豪门抵达华西村
2014/05/23 DOTA
[01:07]2015国际邀请赛 中国区预选赛精彩回顾
2015/06/15 DOTA
python打开网页和暂停实例
2014/09/30 Python
python字符串对其居中显示的方法
2015/07/11 Python
python对list中的每个元素进行某种操作的方法
2018/06/29 Python
python画柱状图--不同颜色并显示数值的方法
2018/12/13 Python
使用Python 正则匹配两个特定字符之间的字符方法
2018/12/24 Python
python操作文件的参数整理
2019/06/11 Python
对python中不同模块(函数、类、变量)的调用详解
2019/07/16 Python
在jupyter notebook 添加 conda 环境的操作详解
2020/04/10 Python
基于注解实现 SpringBoot 接口防刷的方法
2021/03/02 Python
app内嵌H5 webview 本地缓存问题的解决
2020/10/19 HTML / CSS
大学毕业生通用求职信
2013/09/28 职场文书
单位活动策划方案
2014/08/17 职场文书
助人为乐好少年事迹材料
2014/08/18 职场文书
求职自我评价怎么写
2015/03/09 职场文书
《金钱的魔力》教学反思
2016/02/20 职场文书
VUE之图片Base64编码使用ElementUI组件上传
2022/04/09 Vue.js