鍍金池/ 問答/HTML/ Vue-router .push操作無法跳轉(zhuǎn)?

Vue-router .push操作無法跳轉(zhuǎn)?

我創(chuàng)建了一個(gè)login組件
router

export default new Router({
  mode : 'history',
  routes: [
    {
      path: '/login',
      name: 'login',
      component: login
    },
    {
      path: '/test',
      name: 'test',
      component : app
     }
  ]
})```

此時(shí)url地址是 http://localhost:8080/login

<template>
<div>

 <input type="text" class="form-control" placeholder="user" v-model="name">
 <input type="password" class="form-control" placeholder="password" v-model="password">
 <input type="submit" class="btn btn-primary " @click="login" >

</div>
</template>

<script>

export default {
   data(){
     return{
       password:'',
       name:''
     }
     },
   methods:{
      login(){
          const self = this;
          this.$axios({
            method:'post',
            url:'/api/login',
            data : {
              name : this.name,
              password: this.password
            }
          }).then(response =>{
            console.log("response" + response.data)
            if (response ==='0')
              console.log("wrong user")
            else if(response === '1')
              console.log("wrong pwd")
            else if (response === '2'){
              self.$router.replace('/test');
              console.log("OK")
            }

          })
      }
   }
}
現(xiàn)在想當(dāng)response === 2時(shí)候
 this.$router.push('/test/');
url并沒有跳轉(zhuǎn)
回答
編輯回答
半心人

this.$router.push('/test');
'/test/'可能匹配test下級(jí)路由?

2018年1月4日 21:14
編輯回答
萌二代

this.$router.replace('/')

2017年11月15日 00:58
編輯回答
故人嘆

vue建議給組件命名,這樣錯(cuò)誤提示以及使用路由都很方便。命名后,你可以使用this.$router.push({name:'xxxx'}),若需傳參,先在路由定義參數(shù)格式比如path:'/abcd/:id',在push的時(shí)候,增加parmas:{id:'123'}就可以了。

2017年9月17日 03:56
編輯回答
久舊酒

你應(yīng)該把你的router代碼貼下,看下你路由配置。

2018年3月27日 01:42
編輯回答
喜歡你

多半是路由配置出現(xiàn)問題,解決辦法可以到官方路由的詳細(xì)解釋

2018年3月21日 02:58