网络编程 
首页 > 网络编程 > 浏览文章

解决vue.js this.$router.push无效的问题

(编辑:jimmy 日期: 2025/9/20 浏览:3 次 )

如下所示:

login() {
  if(this.email.length > 0 && this.password.length >0) {
   this.$http.post('/api/login', {
    user: this.email,
    password: this.password
   })
   .then(res => {
    let userPwd = res.data
    if(this.password == userPwd) {
     this.$router.push("/")
    } else {
     alert("错误,请重新输入!")
    }
   })
   .catch(err => {
    console.log(err)
   })
  } else {
   alert("输入错误!")
  }
  }

this.$router.push(“/”)不是跳转到主页,而是变成这样:http://127.0.0.1:8080/login"color: #ff0000">解决方案1:

有没有可能是已经跳转了,但是主页判断成了没有登录,然后又跳回来了。

解决方案2:

你这里的 this指向已经不是 vue 的对象啦,可以这么改

login() {
 const self = this;
 if(this.email.length > 0 && this.password.length >0) {
  this.$http.post('/api/login', {
   user: this.email,
   password: this.password
  })
  .then(res => {
   let userPwd = res.data
   if(this.password == userPwd) {
    self.$router.push("/")
   } else {
    alert("错误,请重新输入!")
   }
  })
  .catch(err => {
   console.log(err)
  })
 } else {
  alert("输入错误!")
 }
 }

以上这篇解决vue.js this.$router.push无效的问题就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。

上一篇:vue 纯js监听滚动条到底部的实例讲解
下一篇:JavaScript封装的常用工具类库bee.js用法详解【经典类库】
一句话新闻
微软与英特尔等合作伙伴联合定义“AI PC”:键盘需配有Copilot物理按键
几个月来,英特尔、微软、AMD和其它厂商都在共同推动“AI PC”的想法,朝着更多的AI功能迈进。在近日,英特尔在台北举行的开发者活动中,也宣布了关于AI PC加速计划、新的PC开发者计划和独立硬件供应商计划。
在此次发布会上,英特尔还发布了全新的全新的酷睿Ultra Meteor Lake NUC开发套件,以及联合微软等合作伙伴联合定义“AI PC”的定义标准。