效果图:
目录结构:
共需要修改6个地方,开始前请安装一个依赖:vue-inset-loader
npm i vue-inset-loader
vue-inset-loader的GitHub地址:https://github.com/1977474741/vue-inset-loader
一:新建弹窗文件components/golbalModa.vue
<template> <view class="modal" v-if="globalModal.visible"> <view class="modal-content"> 全局弹窗 </view> </view> </template> <script> import { mapState } from "vuex"; export default { computed: { ...mapState(["globalModal"]), }, methods: {}, }; </script> <style scoped> .modal { position: fixed; top: 0; left: 0; width: 100%; height: 100%; background-color: rgba(0, 0, 0, 0.6); display: flex; align-items: center; justify-content: center; z-index: 999; } .modal-content { background-color: #ffffff; border-radius: 4px; overflow: hidden; padding: 20px; } </style>
二:新建store/index.js
import Vue from "vue"; import Vuex from "vuex"; Vue.use(Vuex); const store = new Vuex.Store({ state: { globalModal: { visible: false, }, }, mutations: { setModal: (state, data) => { state.globalModal = data; }, }, }); export default store;
如果你已经建了store,则在里面进行添加就行了,这里则不赘述。
三:在main.js中引入
import App from "./App"; import Vue from "vue"; import "./uni.promisify.adaptor"; import store from "@/store"; import globalModal from "@/components/globalModal"; Vue.component("globalModal", globalModal); Vue.config.productionTip = false; App.mpType = "app"; const app = new Vue({ ...App, store, }); app.$mount();
四:配置pages.json
{ "insetLoader": { "config": { "confirm": "<globalModal ref='globalModal' />" }, "label": [ "confirm" ], "rootEle": "view" }, "pages": [ { "path": "pages/index/index", "style": { "navigationBarTitleText": "uni-app" } } ], "globalStyle": { "navigationBarTextStyle": "black", "navigationBarTitleText": "uni-app", "navigationBarBackgroundColor": "#F8F8F8", "backgroundColor": "#F8F8F8" }, "uniIdRouter": {} }
注意:这里是重点,只要关心insetLoader这个对象就行了
五:配置vue.config.js
const path = require("path"); module.exports = { configureWebpack: { module: { rules: [ { test: /\.vue$/, use: { loader: path.resolve(__dirname, "./node_modules/vue-inset-loader"), }, }, ], }, }, };
六:在App.vue(或任意.vue文件引用)
<script> export default { onLaunch: function () { this.$store.commit("setModal", { visible: true, }); }, }; </script>
完成以上步骤后请记得一定!一定!!一定!!!要重启项目!!!!否者不会生效!!!!
标签:11,uniapp,vue,自定义,globalModal,loader,Vue,import,store From: https://www.cnblogs.com/iuniko/p/18551393