Vue2 组件

一.全局组件和局部组件

全局注册的组件可以在全局使用;而局部注册的组件只能在其定义的实例内使用。

<div id="app">
    <hello1></hello1>
    <world></world>
</div>
<script>
    Vue.component("hello1",{
        template:"<div>这是全局化组件</div>"
    });

    worldworld={
        template:"<div>我是局部组件</div>"
    };

    var vm=new Vue({
        el:"#app",
        data:null,
        components:{
            "world":worldworld
        }
    });
</script>

二.向组件传递数据和校验
<div id="app">
    <world v-bind:here="country" :prop-c="city"></world>
</div>

<script>
    worldworld={
        template:"<div>here is {{ here }} {{ propC }}</div>",
        //props:["here"]  //写法一:指明可向组件传值 here 这种写法不校验传入值的格式
        props: {          //写法二:指明可向组件传值 here,propA,propB,propC,propD,propE,propF 并校验格式
            here:String,
            // 基础类型检测 (`null` 意思是任何类型都可以)
            propA: Number,
            // 多种类型
            propB: [String, Number],
            // 必传且是字符串
            propC: {
                type: String,
                required: true
            },
            // 数字,有默认值
            propD: {
                type: Number,
                default: 100
            },
            // 数组/对象的默认值应当由一个工厂函数返回
            propE: {
                type: Object,
                default: function () {
                    return { message: 'hello' }
                }
            },
            // 自定义验证函数
            propF: {
                validator: function (value) {
                    return value > 10
                }
            }
        }
    };

    var vm=new Vue({
        el:"#app",
        data:{
            country:"China", //vue2仅允父组件向子组件传值 (子组件要向父组件传值要用vuex)
            city:"Beijing"
        },
        components:{
            "world":worldworld
        }
    });
</script>

三.在组件中注册子组件
<div id="app">
    <p>我的位置是:</p>
    <location :country="country" :city="city"></location>
</div>

<script>
    var cityComponent={
        template:"<div>this is {{ city }}</div>",
        props:["city"]
    };

    var locationComponent= {
        template: `<div>
            <p>this is {{ country }}</p>
            <city :city="city"></city>
        </div>`,
        props: ["country","city"],
        components: {
            "city":cityComponent
        }
    };

    var vm=new Vue({
        el:"#app",
        data:{
            country:"USA",
            city:"Washington D.C."
        },
        components:{
            "location":locationComponent
        }
    });
</script>


四.一个Vue定义的可以动态切换模板的组件component

<div id="app">
    <p>component-一个Vue自定义的组件,使用它可以动态的切换模板。
    </p>
    <component :is="whichTemplate" :count="count"></component>
    <button @click="next">下一个模板</button>
</div>

<script>
    var pA={
        template:'<div>this is {{ name }}  {{ count }}</div>',
        data:function () {
            return{
                name:'pA'
            }
        },
        props:["count"]
    };
    var pB={
        template:'<div>this is pB  {{ count }}</div>',
        props:["count"]
    };
    var pC={
        template:'<div>this is pC  {{ count }}</div>',
        props:["count"]
    };
    var pD={
        template:'<div>this is pD  {{ count }}</div>',
        props:["count"]
    };

    var vm=new Vue({
        el:"#app",
        data:{
            whichTemplate:'p0',
            count:0
        },
        methods:{
            next:function () {
                var current=parseInt(this.whichTemplate.slice(1));
                var next="p"+ ((current+1) % 4);
                this.whichTemplate=next;
                this.count++;
            }
        },
        components:{
            "p0":pA,
            "p1":pB,
            "p2":pC,
            "p3":pD
        }
    });
</script>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue2 组件懒加载是指在使用 Vue.js 开发项目时,将组件按需加载,而不是一次性加载全部组件。这样可以减少初始加载时间,提升页面的加载速度和性能。 在 Vue2 中,懒加载可以通过使用异步组件和 Webpack 的代码分割功能来实现。具体步骤如下: 1. 创建异步组件:在定义组件时,可以使用 Vue 提供的工厂函数 `Vue.component`,将组件定义为一个函数,该函数返回一个 Promise 对象。Promise 对象在异步操作完成时会进行 resolve,将组件作为参数传递给 resolve 函数。 ```javascript Vue.component('MyComponent', () => import('./MyComponent.vue')); ``` 2. 在需要使用组件的地方使用 `v-once` 指令加载组件:通过在组件所在的标签上添加 `v-once` 指令,当组件首次加载完成后会将组件的内容缓存起来,下一次组件重新加载时,会直接使用缓存中的内容,而不再重新渲染。 ```html <template> <div> <my-component v-once></my-component> </div> </template> ``` 3. 配置 Webpack:在使用 Webpack 打包时,需要配置 `vue-loader` 插件来实现组件的懒加载。在 `vue-loader` 配置中添加 `require.ensure` 函数,该函数可以将组件代码分割成独立的文件,需要时再进行加载。 ```javascript module.exports = { ... module: { rules: [ ... { test: /\.vue$/, loader: 'vue-loader' }, ... ] }, resolve: { extensions: ['.js', '.vue'], alias: { 'vue$': 'vue/dist/vue.esm.js' } }, }; ``` 通过以上步骤,在使用 Vue2 开发项目时,可以使用组件懒加载来优化页面加载速度和性能,提升用户体验。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值