vue2.0 常用组件创建

本文介绍了Vue2.0中三种常见的组件创建方法:1) 使用Vue.extend()构造器;2) 组件的全局和局部注册;3) 利用template创建组件,这是项目中常用的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

方法一

使用Vue.extend() 构造器写法

使用基础 Vue 构造器,创建一个“子类”。参数是一个包含组件选项的对象。

data 选项是特例,需要注意 - 在Vue.extend()中它必须是函数

<div id="mount-point"></div>
 var WayONe = Vue.extend({
  template: '<p>{{data}}</p>',
  data: function () {
    return {
      data: '这里是方法一',
    }
  }
})
new WayONe().$mount('#mount-point')

方法二

使用 component

全局注册

<div id="components-demo">
    <way-two></way-two> 
</div>
Vue.component('way-two', { // way-two 是注册组件的名称
  data () {
    return {
      dataTwo: '这里是方法二'
    }
  },
  template: '<p>{{dataTwo}}</p>'
})
// 挂载
new Vue({ el: '#components-demo' })

局部注册

<div id="components-demo">
    <component-a></component-a>
</div>
var ComponentA = {
  data() {
    return {
      dataTwo: '这里是方法二errrr'
    }
  },
  template: '<p>{{dataTwo}}</p>'
}
// 挂载
new Vue({ 
  el: '#components-demo',
  components: {
   'component-a': ComponentA,
  }
})

方法三

使用 template 就是项目中常用的方法

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值