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.组件模板内容可以是模板字符串
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 组件注册的资料请关注站长技术其它相关文章!
utils: // 防抖export const debounce = (func, wait = 3000, immediate = true)...
本文实例讲述了PHP与Web页面交互操作。分享给大家供大家参考,具体如下: Web交...
test.jsp中的代码如下所示 复制代码 代码如下: %@ page language = "java" impor...
分库分表的文章网上非常多,但是大多内容比较零散,以讲解知识点为主,没有完整...
【51CTO.com快译】 Anaconda发行版 随带一种名为Conda的软件包和环境管理系统。...
一、缩小图片大小 当图片很多的时候,减少图片大小是提高下载速度最直接的方法。...
最近因为公司需求,需要ajax post请求并跳转界面,网上搜了一下资料,差不多都是...
table曾经在网页开发中占据着举重若轻的地位布局,即使到了Web2.0我们依旧可以看...
想了解更多内容,请访问: 51CTO和华为官方战略合作共建的鸿蒙技术社区 https://...
原生JS运动实现轮播图 **基本原理:**通过控制包含n张图片的ul的left值来实现图...