最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

vue如何通過(guò)事件的形式調(diào)用全局組件

 更新時(shí)間:2024年09月03日 09:06:24   作者:看客隨心  
這篇文章主要介紹了vue如何通過(guò)事件的形式調(diào)用全局組件問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

vue通過(guò)事件的形式調(diào)用全局組件

創(chuàng)建組件

這里我是寫(xiě)了一個(gè)文件上傳組件

<template>
    <el-dialog :title="title" :visible.sync="open" width="400px" :before-close="handleClose" append-to-body>
        <el-upload ref="upload" :limit="1" accept=".xlsx, .xls" :headers="headers"
            :action="baseApi + url + '?updateSupport=' + updateSupport" :disabled="isUploading"
            :on-progress="handleFileUploadProgress" :on-success="handleFileSuccess" :auto-upload="false" drag>
            <i class="el-icon-upload"></i>
            <div class="el-upload__text">將文件拖到此處,或<em>點(diǎn)擊上傳</em></div>
            <div class="el-upload__tip text-center" slot="tip">
                <div class="el-upload__tip" slot="tip">
                    <el-checkbox v-model="updateSupport"> 是否更新已經(jīng)存在的數(shù)據(jù)</el-checkbox>
                </div>
                <span>僅允許導(dǎo)入xls、xlsx格式文件。</span>
                <el-link v-if="tempUrl" type="primary" :underline="false" style="font-size:12px;vertical-align: baseline;" @click="importTemplate">下載模板</el-link>
            </div>
        </el-upload>
        <div slot="footer" class="dialog-footer">
            <el-button type="primary" @click="submitFileForm">確 定</el-button>
            <el-button @click="handleClose">取 消</el-button>
        </div>
    </el-dialog>
</template>
<script>
import { getToken } from "@/utils/auth";
export default {
    props: {
        url: {
            type: String,
        },
        tempUrl: {
            type: String,
        },
        title: {
            type: String,
            default: "數(shù)據(jù)導(dǎo)入"
        },
        open: {
            type: Boolean,
            default: false
        },
        // 是否更新已經(jīng)存在的用戶數(shù)據(jù)
        updateSupport: {
            type: Number,
            default: 0
        },
        callback: Function
    },
    data() {
        return {
            loading: undefined,
            baseApi: process.env.VUE_APP_BASE_API,
            headers: { Authorization: "Bearer " + getToken() },
            isUploading: false
        }
    },
    methods: {
        // 下載模板操作
        importTemplate() {
            this.download(this.$props.tempUrl, {
            }, `${this.$props.title}模板_${new Date().getTime()}.xlsx`)
        },
 
        // 文件上傳中處理
        handleFileUploadProgress(event, file, fileList) {
            this.isUploading = true;
            this.loading = this.$loading({
                lock: true,
                text: 'Loading',
                spinner: 'el-icon-loading',
                background: 'rgba(0, 0, 0, 0.7)'
            });
        },
 
        // 文件上傳成功處理
        handleFileSuccess(response) {
            this.isUploading = false;
            this.$refs.upload.clearFiles();
            this.$alert("<div style='overflow: auto;overflow-x: hidden;max-height: 70vh;padding: 10px 20px 0;'>" + response.msg + "</div>", "導(dǎo)入結(jié)果", { dangerouslyUseHTMLString: true });
            this.handleClose();
            this.$props.callback && this.$props.callback();
        },
 
        // 提交上傳文件
        submitFileForm() {
            this.$refs.upload.submit();
        },
 
        // 模態(tài)框關(guān)閉
        handleClose() {
            this.loading && this.loading.close();
            this.$refs.upload.clearFiles();
            this.$importModel.hide();
        }
    }
}
</script>
 
<style></style>

全局掛載

通過(guò)vue.use 掛載組件,然后在vue 原型上添加屬性, 再通過(guò)調(diào)用屬性方法傳參的形式控制組件。

import ImportModel from './index.vue'
import { Message } from 'element-ui';
 
const ImportComponent = {
    install(Vue) {
        // 創(chuàng)建組件構(gòu)造函數(shù)
        const ImportInstance = Vue.extend(ImportModel);
        let component;
        const initInstance = () => {
            component = new ImportInstance();
            let mountDom = component.$mount().$el;
            document.body.appendChild(mountDom);
        }
        // 全局掛載組件方法
        Vue.prototype.$importModel = {
            // 顯示模態(tài)框
            show(option) {
                if(!option.url) return Message.error("上傳地址必填!");
                initInstance();
                component.open = true;
                Object.assign(component, option);
            },
            // 關(guān)閉模態(tài)框
            hide() {
                component.open = false;
                let mountDom = component.$mount().$el;
                document.body.removeChild(mountDom);
            }
        };
    }
};
 
export default ImportComponent;

調(diào)用全局屬性方法并傳參

/**
     * 導(dǎo)入操作
     */
    handleImport() {
      this.$importModel.show({
        url: this.queryConfig.importUrl,
        tempUrl: this.queryConfig.importTempUrl,
        title: this.tableName || null,
        callback: () => {
          this.handleQuery();
        }
      })
    },
 

在Vue項(xiàng)目中使用全局組件

1.在 @/components/common創(chuàng)建兩個(gè)組件

  • Logo.vue
  • MyIcon.vue

2.在@/components/common中創(chuàng)建index.js

import Logo from './Logo.vue'
import MyIcon from './MyIcon.vue'

const components = {
    install(Vue){
        Vue.component("Logo",Logo)
        Vue.component("MyIcon",MyIcon)
    }
}
export default components

3.在main.js中加入代碼

//引入全局組件
import Components from '@/components/common'
Vue.use(Components)

4.在任意一個(gè)組件中直接使用這兩個(gè)全局組件

<template>
  <div class="home">
    <my-icon></my-icon>
    <logo></logo>
   </div>
</template>

總結(jié)

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

相關(guān)文章

最新評(píng)論

内黄县| 铁岭市| 游戏| 疏附县| 姚安县| 志丹县| 乐业县| 礼泉县| 西贡区| 扬中市| 赤水市| 新安县| 济宁市| 丽水市| 汶上县| 北宁市| 平利县| 镇雄县| 兖州市| 抚宁县| 南木林县| 浦北县| 衢州市| 合作市| 城口县| 五原县| 韶山市| 高邮市| 策勒县| 射阳县| 仙桃市| 离岛区| 巴彦淖尔市| 宜丰县| 读书| 宜都市| 谢通门县| 宁都县| 区。| 苍南县| 青岛市|