vue3 新建文件store自动导入
2026/5/22 19:13:46 网站建设 项目流程
  1. store下新增个index.js用来做自动导入(pinia使用可参考之前这篇文章
//使用pinia来管理全局状态import{createPinia}from'pinia'// 自动导入所有 store 文件constmodulesFiles=import.meta.glob('./modules/*.js',{eager:true})conststores={}for(constpathinmodulesFiles){constmoduleName=path.replace(/^\.\/modules\/(.*)\.\w+$/,'$1')stores[moduleName]=modulesFiles[path].default}// 安装所有 storeexportconstsetupStore=(app)=>{constpinia=createPinia()app.use(pinia)// 注册所有 storeObject.keys(stores).forEach(key=>{if(stores[key]&&typeofstores[key]==='function'){stores[key]()}})returnpinia}// 导出所有 storeexportdefaultstores
  1. mian.js引入setupStore文件
import{setupStore}from'./store'constapp=createApp(App)setupStore(app)

需要专业的网站建设服务?

联系我们获取免费的网站建设咨询和方案报价,让我们帮助您实现业务目标

立即咨询