亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

vue-extend和vue-component注冊(cè)一個(gè)全局組件方式

 更新時(shí)間:2023年11月09日 10:08:56   作者:donggua_123  
這篇文章主要介紹了vue-extend和vue-component注冊(cè)一個(gè)全局組件方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

extend()是一個(gè)全局構(gòu)造器,生成的是一個(gè)還沒(méi)掛載到頁(yè)面元素上的組件實(shí)例。

在全局掛載插件的形式全局注冊(cè),就需要用到vue.use()方法,官方文檔:

所以插件必須是一個(gè)對(duì)象,并且提供install方法,注冊(cè)的時(shí)候自動(dòng)調(diào)用該方法。

所以組件的寫作步驟就清晰了一些了,實(shí)踐出真知,現(xiàn)在就來(lái)練習(xí)一下寫一個(gè)簡(jiǎn)約版的全局alert彈框

1.在components文件夾下新建alert文件夾

里面首先新建一個(gè)alert.vue文件

<!-- alert -->
<template>
    <div class="alert" v-show="showAlert">
        <p class="msg">{{ msg }}</p>
    </div>
</template>
 
<script>
export default {
    props: {
        msg: {
            type: String,
            default: ''
        },
        showAlert: {
            type: Boolean,
            default: false
        }
    },
    data() {
        return {};
    },
    //方法集合
    methods: {}
};
</script>
<style lang='scss' scoped>
//@import url(); 引入公共css類
.alert {
    position: fixed;
    left: 50%;
    transform: translateX(-50%);
    .msg {
        text-align: center;
        font-size: 20px;
        padding: 5px 10px;
    }
}
</style>

2.然后在alert文件下再新建一個(gè)alert.js文件

寫擴(kuò)展插件的js

import Alert from "./alert.vue";
const ALERT = {
    install(Vue) {
        Vue.component('alert', Alert); // 一定先注冊(cè)
 
        Vue.prototype.$alert = (text) => {
            let VueAlert = Vue.extend({
                data() {
                    return {
                        msg: '',
                        showAlert: false
                    }
                },
                render(h) {
                    let props = {
                        msg: this.msg,
                        showAlert: this.showAlert,
                    }
                    return h('alert', {
                        props
                    })
                }
            });
            let newAlert = new VueAlert();
            let vm = newAlert.$mount();
            let el = vm.$el;
            document.body.appendChild(el); // 添加到頁(yè)面中
            vm.showAlert = true;
            vm.msg = text;
        }
    }
}
export default ALERT;

3.在main.js中引入

、、、、
import diyAlert from '@/components/alert/alert'; // extentd寫alert
、、、
Vue.use(diyAlert);

4.這樣就可以在全局調(diào)用了

<!-- extend寫message -->
<template>
    <div class="extentd">
         <el-button type="primary" @click="alert">alert</el-button>
    </div>
</template>
 
<script>
export default {
    data() {
        return {};
    },
    //方法集合
    methods: {
        alert() {
            this.$alert('hahahah');
        }
    }
};
</script>
<style lang='scss' scoped>
//@import url(); 引入公共css類
</style>

剩下的就是在此基礎(chǔ)上進(jìn)行功能性擴(kuò)展了,比如關(guān)閉,動(dòng)效等,就簡(jiǎn)單了。

總結(jié)

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論