最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How can I navigate to another route from actions in Vue's Pinia store? - Stack Overflow

programmeradmin2浏览0评论

I'm currently working in a Vue 3 project.

Using the this.$router.push({}) doesn't seem to work in the Pinia store.

I have also tried importing the useRouter --> import { useRouter } from "vue-router"; to simply use router.push, but yet nothing still seems to work.

I don't know what could be the problem or what I should use to navigate my routes from the actions in the Pinia store.

import { defineStore } from "pinia";
import axios from "axios";

export const SignupStore = defineStore("signup", {
  state: () => ({
    fullName: "",
    dob: "",
    email: "",
    password: "",
    passwordConfirm: "",
    gender: "",
    program: "",
    genderOptions: ["Male", "Female"],
    degreePrograms: ["Degree program", "HND program", "Masters program"],
    isSignupCorrect: false,
  }),

  actions: {
    async signup() {
      let dob = new Date(this.dob).toISOString().substring(0, 10);
      let gender = this.gender.substring(0, 1);
      let program = this.program;
      if (program == "Degree program") {
        program = "De";
      } else if (program == "HND program") {
        program = "Hn";
      } else {
        program = "Ms";
      }

      console.log(dob);
      console.log(program);

      await axios
        .post("register/", {
          full_name: this.fullName,
          email: this.email,
          password: this.password,
          gender: gender,
          dob: dob,
          program: program,
        })
        .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");
          }
        });
    },
  },
});

I'm currently working in a Vue 3 project.

Using the this.$router.push({}) doesn't seem to work in the Pinia store.

I have also tried importing the useRouter --> import { useRouter } from "vue-router"; to simply use router.push, but yet nothing still seems to work.

I don't know what could be the problem or what I should use to navigate my routes from the actions in the Pinia store.

import { defineStore } from "pinia";
import axios from "axios";

export const SignupStore = defineStore("signup", {
  state: () => ({
    fullName: "",
    dob: "",
    email: "",
    password: "",
    passwordConfirm: "",
    gender: "",
    program: "",
    genderOptions: ["Male", "Female"],
    degreePrograms: ["Degree program", "HND program", "Masters program"],
    isSignupCorrect: false,
  }),

  actions: {
    async signup() {
      let dob = new Date(this.dob).toISOString().substring(0, 10);
      let gender = this.gender.substring(0, 1);
      let program = this.program;
      if (program == "Degree program") {
        program = "De";
      } else if (program == "HND program") {
        program = "Hn";
      } else {
        program = "Ms";
      }

      console.log(dob);
      console.log(program);

      await axios
        .post("register/", {
          full_name: this.fullName,
          email: this.email,
          password: this.password,
          gender: gender,
          dob: dob,
          program: program,
        })
        .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");
          }
        });
    },
  },
});

Share Improve this question edited Apr 15, 2022 at 14:57 Njita Arnaud asked Apr 15, 2022 at 12:17 Njita ArnaudNjita Arnaud 572 silver badges9 bronze badges 1
  • You need to import YOUR router, rather than the Vue instance. For example, import router from '@/router'; where ./src/router.js implements Vue's router. – Alicia Sykes Commented Apr 15, 2022 at 12:22
Add a ment  | 

2 Answers 2

Reset to default 6

The only place that you need to import vue-router is in your router file.

You can then use it anywhere in the app, by importing YOUR router file (which implrments vue-router).

So all you need to do in your store, is import your router, then call the push method, like you're doing, and it should work.

Another alternative is to load the router as a plugin.

In your store/index,js:

pinia.use(({ store }) => { store.router = markRaw(router) })

Then it will be accessible in your store modules like: this.router.push

Repeating the caveat that you must import YOUR router, such as from src/router (Quasar)

发布评论

评论列表(0)

  1. 暂无评论