报错问题:
store() 的调用需要放在 Pinia 安装后才会执行的函数中才不会报错。
路由拦截
import store from '@/store'
router.beforeEach(async (to, _from, next) => {
const { user, permission } = store()
// ...
next()
})
axios请求头添加token
import axios from 'axios'
import useUserStore from '@/store/modules/user'
const service = axios.create({
baseURL: `${import.meta.env.VITE_APP_BASE_API}`,
timeout: 20000
})
service.interceptors.request.use(
(config: any) => {
const user = useUserStore()
config.headers.Authorization = user.getToken
return config
},
(err) => Promise.reject(err)
)