vue js登录成功后如何重定向到dashboard组件页面(登录组件)?

How to redirect to the dashboard component page after login successfully(login component) in vue js?

您好,我想将我的登录组件页面重定向到登录成功的仪表板组件。我已经通过我的本地 API 进行了响应,状态代码为 400 和状态代码 200,但不知道如何在成功登录后重定向。

组件脚本代码如下:

    import axios from 'axios'
    export default {
      name: 'Login',
      data () {
        return {
          email: '',
          password: '',
          error: ''
        }
      },
      methods: {
        login () {
          axios.post('http://ztlab01/db-admin/app/v1/login', {
            email: this.email,
            password: this.password
          }, {'Access-Control-Allow-Origin': '*'})
            .then((response) => {
              console.log(response.data)
              if (response.data.code === 400) {
                this.error = 'Invalid Credentials'
              }
              if (response.data.code === 200) {
                this.error = ''
              }
            })
            .catch(error => {
              console.log(error)
            })
        }
      }
    }

如果您使用 vue-router,您应该使用 router.go(path) (VueJS < 2.0) 导航到任何特定的路线。

可以使用 this.$router.

从组件内访问路由器

router.go() 在 VueJS 2.0 中更改。您现在可以使用 router.push({ name: "yourroutename"})router.push("yourroutename") 进行重定向。

否则,window.location.href = 'some url'; 适用于非 single-page 应用程序。

更多帮助链接

https://router.vuejs.org/en/essentials/redirect-and-alias.html