0

伙计们,我在 Vue 3 和 Vite 中遇到问题,我正在尝试使用路由器,但我遇到了问题,因为他们没有找到它。

    props: ["usuario", "senha"],

  setup(props) {
    const mensagemErro = ref("");

    async function login() {
      try {
        const { data } = await services.post("auth/login", {
          userName: props.usuario,
          password: props.senha,
        });
        console.log(data);
        const { token, userName } = data;
        window.localStorage.setItem("token", token);
        this.$router.push("/home");
      } catch (error) {
        console.log(error);
      }
    }

    return {
      login,
      mensagemErro,
    };
  },
4

1 回答 1

2

您应该使用可组合功能useRouter来获取实例路由器:

import {useRouter} from 'vue-router'
export default{
    props: ["usuario", "senha"],

  setup(props) {
    const mensagemErro = ref("");
   const router=userRouter();
   
   async function login() {
      try {
        const { data } = await services.post("auth/login", {
          userName: props.usuario,
          password: props.senha,
        });
        console.log(data);
        const { token, userName } = data;
        window.localStorage.setItem("token", token);
        router.push("/home");
      } catch (error) {
        console.log(error);
      }
    }

    return {
      login,
      mensagemErro,
    };
  },

于 2021-04-24T23:10:02.683 回答