0

Guys I have a problem in Vue 3 and Vite, I'm trying to use the router but I have a problem because they didn't find it.

    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,
    };
  },
1
  • 1
    I'm guessing this is a scoping issue with the this property. Try changing your login function to an arrow function const login = async () =>{} Commented Apr 24, 2021 at 23:09

1 Answer 1

2

You should use the composable function useRouter to get the instance router :

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,
    };
  },

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.