vue如何通過事件的形式調(diào)用全局組件
vue通過事件的形式調(diào)用全局組件
創(chuàng)建組件
這里我是寫了一個(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>全局掛載
通過vue.use 掛載組件,然后在vue 原型上添加屬性, 再通過調(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使用三方工具vueUse實(shí)現(xiàn)虛擬列表
其實(shí)采用vueUse中的useVirtualList方法同樣可以實(shí)現(xiàn)虛擬列表,這篇文章小編就來和大家詳細(xì)介紹一下如何使用vueUse實(shí)現(xiàn)簡(jiǎn)單的虛擬列表效果吧2024-04-04
基于Vue CSR的微前端實(shí)現(xiàn)方案實(shí)踐
這篇文章主要介紹了基于Vue CSR的微前端實(shí)現(xiàn)方案實(shí)踐,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-05-05
Vue項(xiàng)目報(bào)錯(cuò):Uncaught?SyntaxError:?Unexpected?token?'<&a
最近在做vue項(xiàng)目時(shí),需要引入一個(gè)第三方的js文件,在index.html中通過以下方式引入JS文件編譯后就報(bào)了這個(gè)問題,這篇文章主要給大家介紹了關(guān)于Vue項(xiàng)目報(bào)錯(cuò):Uncaught?SyntaxError:?Unexpected?token?'<'的解決方法,需要的朋友可以參考下2022-08-08
去除element-ui下拉框的下拉箭頭的實(shí)現(xiàn)
我們最開始拿到的element-ui是帶有下拉箭頭的,那么如何去除element-ui下拉框的下拉箭頭的實(shí)現(xiàn),本文就詳細(xì)的介紹一下,感興趣的可以了解一下2023-08-08
antd Select下拉菜單動(dòng)態(tài)添加option里的內(nèi)容操作
這篇文章主要介紹了antd Select下拉菜單動(dòng)態(tài)添加option里的內(nèi)容操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-11-11
Vue+Element Plus實(shí)現(xiàn)自定義日期選擇器
這篇文章主要為大家詳細(xì)介紹了如何基于Vue和Element Plus提供的現(xiàn)有組件,設(shè)計(jì)并實(shí)現(xiàn)了一個(gè)自定義的日期選擇器組件,感興趣的小伙伴可以參考一下2024-12-12
vue實(shí)現(xiàn)登錄頁面的驗(yàn)證碼以及驗(yàn)證過程解析(面向新手)
這篇文章主要介紹了vue實(shí)現(xiàn)登錄頁面的驗(yàn)證碼以及驗(yàn)證過程解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-08-08

