【发布时间】:2022-01-16 20:46:02
【问题描述】:
我对 Vue 3 和 Vuex 有一些问题。 我在登录我的 Vuex 文件时尝试重定向用户,但它没有按预期工作。 它没有返回任何错误,它正在更改链接,但没有重定向到另一个页面。
我的动作是这样的;
actions: {
async login(commit: any, payload: any ) {
const cookie_token = useCookies();
API.post('/login', {
email: payload.email.value,
password: payload.password.value
})
.then((response: any) => {
notif.success('Welcome back, ' + response.data.player.name)
cookie.set('user', response.data)
commit.commit('loginSuccess', response.data)
router.push({
name: 'index',
})
}).catch((e) => (
console.log(e.message)
)
)
}
},
路由器正在获取定义路由的文件。
这是我的完整路由器文件:
import {
createRouter as createClientRouter,
createWebHistory,
} from 'vue-router'
import * as NProgress from 'nprogress'
// import routes from 'pages-generated'
import type { RouteRecordRaw } from "vue-router";
// Then we can define our routes
const routes: RouteRecordRaw[] = [
// This is a simple route
{
component: () => import("/@src/pages/index.vue"),
name: "index",
path: "/",
props: true,
},
{
component: () => import("/@src/pages/auth.vue"),
path: "/auth",
props: true,
children: [
{
component: () => import("/@src/pages/auth/login.vue"),
name: "auth-login",
path: "login-1",
props: true,
},
{
component: () => import("/@src/pages/auth/login.vue"),
name: "auth-signup",
path: "singup",
props: true,
},
],
},
{
component: () => import("/@src/pages/[...all].vue"),
name: "404",
path: "/:all(.*)",
props: true,
},
];
export function createRouter() {
const router = createClientRouter({
history: createWebHistory(),
routes,
})
/**
* Handle NProgress display on-page changes
*/
router.beforeEach(() => {
NProgress.start()
})
router.afterEach(() => {
NProgress.done()
})
return router
}
export default createRouter()
请记住它正在处理其他文件,我可以看到这里触发了路由器,但没有更改页面,只有在 vuex 上不起作用。如果它不起作用,为什么没有错误?
【问题讨论】:
标签: javascript vue.js vuex vuejs3