我正在使用 pinia 和 vue-router 4.x ,但我在商店中使用它们时遇到问题。 每个都独立工作,但不能一起工作。
如果我使用
import router from '../router'
路由器可以工作,但 pinia 因错误而失败
Uncaught ReferenceError: Cannot access 'useAuthStore' before initialization
at axiosroot.ts
@line let authStore = useAuthStore(pinia);
//这里是axiosroot.ts
import axios from "axios";
import {useAuthStore} from '../stores/auth-store'
import { createPinia } from "pinia";
const pinia=createPinia();
let authStore = useAuthStore(pinia);
const url = "http://127.0.0.1:8000/api";
const myToken =authStore.getToken;
export default axios.create({
url,
headers:{"Accept":"application/json"},
});
当我从 vue-router 导入路由器时
useRouter
未定义
import {useRouter} from 'vue-router'
const router =useRouter();
错误
Uncaught TypeError: Cannot read properties of undefined (reading 'push')
---
error @ line router.push({name:'Login'})
//这是剩下的相关代码
import { defineStore, acceptHMRUpdate } from "pinia";
//import router from '../router'
import {useRouter} from 'vue-router'
const router =useRouter();
export const useAuthStore = defineStore({
id: "user",
actions: {
LogOut(payload) {
// this.DELETE_TOKEN(null);
// this.UPDATE_LOGIN_STATUS(false);
router.push({name:'Login'})
},
},
});
路由器必须作为 pinia 中的插件使用。我在 pinia.documentation 中读到了这篇文章 https://pinia.vuejs.org/core-concepts/plugins.html
当添加外部属性、来自其他库的类实例或简单的非反应性内容时,您应该在将对象传递给 pinia 之前使用 markRaw() 包装该对象。以下是将路由器添加到每个商店的示例:
import { markRaw } from 'vue'
// adapt this based on where your router is
import { router } from './router'
pinia.use(({ store }) => {
store.router = markRaw(router)
})
这会将 pinia 添加到您创建的每个商店中。 main.ts 中的配置与 vue 插件相同。
import { createApp, markRaw } from 'vue';
import { createPinia } from 'pinia';
import App from './app/App.vue';
import { router } from './modules/router/router';
const app = createApp(App);
const pinia = createPinia();
pinia.use(({ store }) => {
store.$router = markRaw(router)
});
app.use(router)
现在您可以访问商店中的路由器了。
export const useMyStore = defineStore("myStore", {
state: () => {
return {};
},
actions: {
myAction() {
this.$router.push({ name: 'Home' });
},
});
在 main.js:
import { createApp, markRaw } from 'vue'
import { createPinia } from 'pinia'
import App from './App.vue'
import router from './router'
const pinia = createPinia()
pinia.use(({ store }) => {
store.router = markRaw(router)
})
const app = createApp(App)
app.use(pinia)
app.use(router)
app.mount('#app')
在您的商店中推送路线:
this.router.push({ name: 'home' });
我解决了将商店包装在函数中的问题:
/*** THIS DOES NOT WORK ****/
const router = useRouter();
export const useLoginStore = defineStore("login", {
state: () => ({
loading: false,
error: "",
}),
actions: {
loginAction(credentials: AuthenticationRequest): Promise<void> {
await login(credentials);
router.replace("/")
},
},
});
/*** This actually works ***/
export default function useLoginStore() {
const router = useRouter();
return defineStore("login", {
state: () => ({
loading: false,
error: "",
}),
actions: {
loginAction(credentials: AuthenticationRequest): Promise<void> {
await login(credentials);
router.replace("/")
},
},
})();
}
对于任何使用 quasar v2 的人。这对我有用
在 /src/stores/index.js 文件中,添加以下内容
import { markRaw } from 'vue'
import { route } from 'quasar/wrappers'
pinia.use(({ store }) => {
// important! dont add a $router here
store.router = markRaw(route)
})
然后在你的 src/stores/[yourStore].js 文件中使用
this.router.push("/") // important! dont add a $router here
我在尝试按照最上面答案中的建议添加 $router 时遇到了麻烦
对于任何使用 quasar Framework v2 的人
找到 /src/stores/index.js 文件并编辑
import { useRouter } from 'vue-router' ---> import this
// You can add Pinia plugins here
// pinia.use(SomePiniaPlugin)
pinia.router = useRouter() ---> add this line
在商店文件中
this.router.push({ name: 'Home' }) --> use this
如果您需要在 Pinia 中使用路由器而不传递方法、道具或对象,那么更干净的解决方案可能是这样。
import './assets/main.css'
import { createApp } from 'vue'
import { createPinia } from 'pinia'
import App from './App.vue'
import router from './router'
const pinia = createPinia()
const app = createApp(App)
app.use(pinia)
app.use(router)
app.provide('router', router);
app.mount('#app')
然后在你的商店:
import { inject } from "vue";
actions: {
navigate() {
const router = inject('router');
router.push({
name: "customers",
params: { page: this.currentPage },
});
},
},
pinia.use() 更灵活,允许您传递插件、方法、对象、道具。 两者都适合将路由器传递给pinia。
如果您在商店操作中而不是在模块顶部声明
const router = useRouter();
,它应该可以工作。
对于
const store = useSomeStore()
声明也是一样的。它们不能在模块路由中使用,并且通常在组件 setup
函数和存储操作中使用。
您不需要需要创建插件,但如果您可能在所有商店中使用路由器,那么仍然值得这样做,如此答案
中所述