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组件生命周期运行原理解析
Nov 25 Vue.js
element-plus一个vue3.xUI框架(element-ui的3.x 版初体验)
Dec 02 Vue.js
vue表单验证之禁止input输入框输入空格
Dec 03 Vue.js
vue基于Echarts的拖拽数据可视化功能实现
Dec 04 Vue.js
Vue如何实现验证码输入交互
Dec 07 Vue.js
Vue 事件的$event参数=事件的值案例
Jan 29 Vue.js
vue项目配置 webpack-obfuscator 进行代码加密混淆的实现
Feb 26 Vue.js
详解vue身份认证管理和租户管理
May 25 Vue.js
vue+element ui实现锚点定位
Jun 29 Vue.js
vue route新窗口跳转页面并且携带与接收参数
Apr 10 Vue.js
vue-treeselect的基本用法以及解决点击无法出现拉下菜单
Apr 30 Vue.js
Vue3实现简易音乐播放器组件
Aug 14 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
让你的WINDOWS同时支持MYSQL4,MYSQL4.1,MYSQL5X
2006/12/06 PHP
php将csv文件导入到mysql数据库的方法
2014/12/24 PHP
php浏览历史记录的方法
2015/03/10 PHP
php curl 上传文件代码实例
2015/04/27 PHP
PHP设计模式之简单工厂和工厂模式实例分析
2019/03/25 PHP
JQuery操作元素的css样式
2015/03/09 Javascript
localResizeIMG先压缩后使用ajax无刷新上传(移动端)
2015/08/11 Javascript
js简单网速测试方法完整实例
2015/12/15 Javascript
Bootstrap表格和栅格分页实例详解
2016/05/20 Javascript
jquery的ajax提交form表单的两种方法小结(推荐)
2016/05/25 Javascript
Angualrjs 表单验证的两种方式(失去焦点验证和点击提交验证)
2017/05/09 Javascript
H5实现仿flash效果的实现代码
2017/09/29 Javascript
vue-cli配置文件——config篇
2018/01/04 Javascript
微信小程序中使用Async-await方法异步请求变为同步请求方法
2019/03/28 Javascript
如何利用node.js开发一个生成逐帧动画的小工具
2019/12/01 Javascript
node koa2 ssr项目搭建的方法步骤
2020/12/11 Javascript
Python多线程编程(二):启动线程的两种方法
2015/04/05 Python
Django项目实战之用户头像上传与访问的示例
2018/04/21 Python
python实现从文件中读取数据并绘制成 x y 轴图形的方法
2018/10/14 Python
使用Python的OpenCV模块识别滑动验证码的缺口(推荐)
2019/05/10 Python
Python用字典构建多级菜单功能
2019/07/11 Python
python Web flask 视图内容和模板实现代码
2019/08/23 Python
Python实现自动打开电脑应用的示例代码
2020/04/17 Python
清除canvas画布内容(点擦除+线擦除)
2020/08/12 HTML / CSS
香港永安旅游网:Wing On Travel
2017/04/10 全球购物
德国网上宠物店:Zoobio
2018/05/23 全球购物
八年级物理教学反思
2014/01/19 职场文书
电子装配专业毕业生求职信
2014/04/23 职场文书
残疾人小组计划书
2014/04/27 职场文书
国庆65周年演讲稿:回首往昔,展望未来
2014/09/21 职场文书
自主招生学校推荐信范文
2015/03/26 职场文书
英文投诉信格式
2015/07/03 职场文书
2015年汽车销售员工作总结
2015/07/24 职场文书
智慧人生:永远不需要向任何人解释你自己
2019/08/20 职场文书
Vue实现导入Excel功能步骤详解
2021/07/03 Vue.js
SQL Server中搜索特定的对象
2022/05/25 SQL Server