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項目報錯:Uncaught?SyntaxError:?Unexpected?token?'<&a
最近在做vue項目時,需要引入一個第三方的js文件,在index.html中通過以下方式引入JS文件編譯后就報了這個問題,這篇文章主要給大家介紹了關于Vue項目報錯:Uncaught?SyntaxError:?Unexpected?token?'<'的解決方法,需要的朋友可以參考下2022-08-08antd Select下拉菜單動態(tài)添加option里的內容操作
這篇文章主要介紹了antd Select下拉菜單動態(tài)添加option里的內容操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-11-11