VUE创建使用组件的5种方式

VUE创建使用组件的5种方式

  • 第一种,以.$mount挂载形式

 

    // 创建构造器
    var Profile = Vue.extend({
        template: '<p>{{firstName}} {{lastName}} aka {{alias}}</p>',
        data: function () {
            return {
                firstName: 'Walter',
                lastName: 'White',
                alias: 'Heisenberg'
            }
        }
    })

// 创建 Profile 实例,并挂载到一个元素上。 

//将组件添加到ID为mount-point的标签上显示

new Profile().$mount('#mount-point')

 

  • 第二种,以id的形式

js代码

Vue.component("div1",{
    template: '#template'
})

html代码

<template id="template">
    <div>我是组件</div>
</template>

 

  • 全局组件,传入Vue.extend对象

Vue.component("p-1",Profile);

 

使用方式,在html内添加标签

<p-1></p-1>

  • 第四种 ,Vue.component  的第二个参数直接传入对象,第二个参数传入后会自动调用 Vue.extend

Vue.component("p-2",{

        template: '<p>{{firstName}} {{lastName}} aka {{alias}}</p>',

        data: function () {

            return {

                firstName: 'Walter',

                lastName: 'White',

                alias: 'Heisenberg'

            }

        }

});

//调用方式,在html内添加如下标签

<p-2></p-2>

 

  • 五种 ,使用components参数局部添加

 

调用方式

<button1></button1>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值