vue如何通過(guò)事件的形式調(diào)用全局組件
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 components3.在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)文章
Vue項(xiàng)目安裝less和less-loader的詳細(xì)步驟
這篇文章主要介紹了Vue項(xiàng)目安裝less和less-loader的詳細(xì)步驟,本文分步驟結(jié)合示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-12-12
vue+elementUi 實(shí)現(xiàn)密碼顯示/隱藏+小圖標(biāo)變化功能
這篇文章主要介紹了vue+elementUi 實(shí)現(xiàn)密碼顯示/隱藏+小圖標(biāo)變化功能,需本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-01-01
vue $mount 和 el的區(qū)別說(shuō)明
這篇文章主要介紹了vue $mount 和 el的區(qū)別說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-09-09
vue項(xiàng)目 npm run build 打包項(xiàng)目防止瀏覽器緩存的操作方法
這篇文章主要介紹了vue項(xiàng)目 npm run build 打包項(xiàng)目防止瀏覽器緩存的操作方法,本文給大家推薦兩種方法,每種方法通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2022-08-08
vue項(xiàng)目檢測(cè)依賴包是否有使用的問(wèn)題
這篇文章主要介紹了vue項(xiàng)目檢測(cè)依賴包是否有使用的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-08-08
解決ElementUI組件中el-upload上傳圖片不顯示問(wèn)題
這篇文章主要介紹了解決ElementUI組件中el-upload上傳圖片不顯示問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-10-10
vue el-switch綁定數(shù)值時(shí)需要注意的問(wèn)題
在Vue中使用`el-switch`組件時(shí),綁定數(shù)值類型時(shí)應(yīng)使用布爾值(true/false),而綁定字符串類型時(shí)應(yīng)使用字符串('true'/'false')2024-12-12
vue 使用element-ui中的Notification自定義按鈕并實(shí)現(xiàn)關(guān)閉功能及如何處理多個(gè)通知
這篇文章主要介紹了vue 使用element-ui中的Notification自定義按鈕并實(shí)現(xiàn)關(guān)閉功能及如何處理多個(gè)通知,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-08-08

