我在一个Vue 3项目中工作。
我创建了一家专卖店,专门为我的注册组件与Pinia。在商店的操作中,我编写了一个函数,它向后端发出一个post请求,并被期望导航到下面的页面,以防从响应获得的结果状态是"201“。
但是我现在的问题是,它没有进入我的"if函数“,而我在"if函数”之前做了一个结果状态的日志,它很好地打印出"201“,因此通常应该执行在我的"if”函数中写的router.push语句。
我尝试过在if中登录并在控制台中打印出来。但是router.push语句没有被执行,我不知道为什么。我做了一个简单的按钮,在商店中调用一个函数,这个函数只需使用相同的router.push进行路由,它就可以正常工作。那到底是怎么回事?
如果我能问的话,我的问题可能是什么?下面是代码段。
<>
import { defineStore } from "pinia";
import axios from "axios";
export const SignupStore = defineStore("signup", {
state: () => ({
fullName: "",
dob: "",
email: "",
password: "",
confirmPassword: "",
gender: "",
program: "",
genderOptions: ["Male", "Female"],
degreePrograms: ["Degree program", "HND program", "Masters program"],
isSignupCorrect: false,
}),
actions: {
async signup() {
let dobx = new Date(this.dob).toISOString().substring(0, 10);
let genderx = this.gender.substring(0, 1);
let programx = this.program;
if (programx == "Degree program") {
programx = "De";
} else if (programx == "HND program") {
programx = "Hn";
} else {
programx = "Ms";
}
await axios
.post("register/", {
full_name: this.fullName,
email: this.email,
password: this.password,
gender: genderx,
dob: dobx,
program: programx,
})
.then((response) => {
console.log('response status is ' + response.status)
if (response.status == 201) {
router.push({ name: "AdmissionDashboard" });
}
})
.catch((error) => {
if (error.response) {
console.log("signup error traced");
}
});
},
},
});
<>
<script setup>
import NavBar from "../components/NavBar.vue";
import { ref } from "@vue/reactivity";
import axios from "axios";
import { SignupStore } from "../Stores/SignupStore";
const user = SignupStore()
<>
<va-button
:disabled="isDisabled"
class="submit-btn"
color="#03c460"
size="large"
@click="user.signup"
>Create account</va-button
>
<>
import { createRouter, createWebHistory } from "vue-router";
import SignupPage from '../Views/SignupPage.vue'
import LandingPage from '../Views/LandingPage.vue'
import AdmissionInfo from '../Views/AdmissionInfo.vue'
import SignInPage from '../Views/SignInPage.vue'
import PlatformCharges from '../Views/PlatformServiceCharge.vue'
import AdmissionDashboard from '../Views/AdmissionDashboard.vue'
import ApplicationPage from '../Views/ApplicationPage.vue'
import Test from '../Views/test.vue'
const routes = [
{
path: '/signup',
name: 'SignupPage',
component: SignupPage
},
{
path: '/test',
name: 'test',
component: Test
},
{
path: '/',
name: 'LandingPage',
component: LandingPage
},
{
path: '/admission-info',
name: 'AdmissionInfo',
component: AdmissionInfo
},
{
path: '/sign-in',
name: 'SignInPage',
component: SignInPage
},
{
path: '/platform-charges',
name: 'PlatformCharges',
component: PlatformCharges
},
{
path: '/admission-dashboard',
name: 'AdmissionDashboard',
component: AdmissionDashboard
},
{
path: '/app-page',
name: 'ApplicationPage',
component: ApplicationPage
},
]
const router = createRouter({
history: createWebHistory(),
routes
})
export default router
发布于 2022-04-15 14:31:08
据我所见,您在signup()中没有提到“路由器”。您需要在存储文件中导入路由器。
import { defineStore } from "pinia";
import axios from "axios";
import router from "yourRouterPath";https://stackoverflow.com/questions/71885107
复制相似问题