欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

vue如何通過事件的形式調用全局組件

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

vue通過事件的形式調用全局組件

創(chuàng)建組件

這里我是寫了一個文件上傳組件

<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>點擊上傳</em></div>
            <div class="el-upload__tip text-center" slot="tip">
                <div class="el-upload__tip" slot="tip">
                    <el-checkbox v-model="updateSupport"> 是否更新已經存在的數據</el-checkbox>
                </div>
                <span>僅允許導入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: "數據導入"
        },
        open: {
            type: Boolean,
            default: false
        },
        // 是否更新已經存在的用戶數據
        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>", "導入結果", { dangerouslyUseHTMLString: true });
            this.handleClose();
            this.$props.callback && this.$props.callback();
        },
 
        // 提交上傳文件
        submitFileForm() {
            this.$refs.upload.submit();
        },
 
        // 模態(tài)框關閉
        handleClose() {
            this.loading && this.loading.close();
            this.$refs.upload.clearFiles();
            this.$importModel.hide();
        }
    }
}
</script>
 
<style></style>

全局掛載

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

import ImportModel from './index.vue'
import { Message } from 'element-ui';
 
const ImportComponent = {
    install(Vue) {
        // 創(chuàng)建組件構造函數
        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);
            },
            // 關閉模態(tài)框
            hide() {
                component.open = false;
                let mountDom = component.$mount().$el;
                document.body.removeChild(mountDom);
            }
        };
    }
};
 
export default ImportComponent;

調用全局屬性方法并傳參

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

在Vue項目中使用全局組件

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

  • 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.在任意一個組件中直接使用這兩個全局組件

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

總結

以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關文章

  • Vue使用三方工具vueUse實現虛擬列表

    Vue使用三方工具vueUse實現虛擬列表

    其實采用vueUse中的useVirtualList方法同樣可以實現虛擬列表,這篇文章小編就來和大家詳細介紹一下如何使用vueUse實現簡單的虛擬列表效果吧
    2024-04-04
  • 基于Vue CSR的微前端實現方案實踐

    基于Vue CSR的微前端實現方案實踐

    這篇文章主要介紹了基于Vue CSR的微前端實現方案實踐,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-05-05
  • Vue項目報錯:Uncaught?SyntaxError:?Unexpected?token?'<'的解決方法

    Vue項目報錯:Uncaught?SyntaxError:?Unexpected?token?'<&a

    最近在做vue項目時,需要引入一個第三方的js文件,在index.html中通過以下方式引入JS文件編譯后就報了這個問題,這篇文章主要給大家介紹了關于Vue項目報錯:Uncaught?SyntaxError:?Unexpected?token?'<'的解決方法,需要的朋友可以參考下
    2022-08-08
  • vue.js如何將echarts封裝為組件一鍵使用詳解

    vue.js如何將echarts封裝為組件一鍵使用詳解

    Echarts 、 Remodal和Pikaday是我們在開發(fā)后臺管理類網站時常用的三個第三方組件,下面這篇文章主要給大家介紹了關于vue.js如何將echarts封裝為組件一鍵使用的相關資料,文中通過示例代碼介紹的非常詳細,需要的朋友可以參考下。
    2017-10-10
  • 去除element-ui下拉框的下拉箭頭的實現

    去除element-ui下拉框的下拉箭頭的實現

    我們最開始拿到的element-ui是帶有下拉箭頭的,那么如何去除element-ui下拉框的下拉箭頭的實現,本文就詳細的介紹一下,感興趣的可以了解一下
    2023-08-08
  • antd Select下拉菜單動態(tài)添加option里的內容操作

    antd Select下拉菜單動態(tài)添加option里的內容操作

    這篇文章主要介紹了antd Select下拉菜單動態(tài)添加option里的內容操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-11-11
  • Vue+Element Plus實現自定義日期選擇器

    Vue+Element Plus實現自定義日期選擇器

    這篇文章主要為大家詳細介紹了如何基于Vue和Element Plus提供的現有組件,設計并實現了一個自定義的日期選擇器組件,感興趣的小伙伴可以參考一下
    2024-12-12
  • vue中el-table表格的表頭操作代碼

    vue中el-table表格的表頭操作代碼

    本文通過實例代碼介紹對el-table表格的表頭操作方法,本文結合實例代碼給大家介紹的非常詳細,感興趣的朋友跟隨小編一起看看吧
    2024-03-03
  • vue實現登錄頁面的驗證碼以及驗證過程解析(面向新手)

    vue實現登錄頁面的驗證碼以及驗證過程解析(面向新手)

    這篇文章主要介紹了vue實現登錄頁面的驗證碼以及驗證過程解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2019-08-08
  • vue數組雙向綁定問題及$set用法說明

    vue數組雙向綁定問題及$set用法說明

    這篇文章主要介紹了vue數組雙向綁定問題及$set用法說明,具有很好的參考價值,希望大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-09-09

最新評論