- 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- mian.js引入setupStore文件
import{setupStore}from'./store'constapp=createApp(App)setupStore(app)