vue3 解决警告: Promise returned from xxx is ignored 和 $router未定义

在vue-cli脚手架中创建一个AxiosMethod.js文件用来放公共方法。

开始的代码如下:

export  function post() {
    await axios.post("http://localhost:9090/checkFace").then(function (res) {
        if (res.data.code == 1) {
            ElMessageBox.alert('识别成功', '系统提示', {
                confirmButtonText: 'OK'
            })
            this.$router.push("/");
        } else {
            ElMessageBox.alert('识别失败', '系统提示', {
                confirmButtonText: 'OK'
            })
        }
    }).catch(function (err) {
        console.log(err)
    });
}

当开始测试的时候出现了  警告: Promise returned from xxx is ignored,执行成功但是无法跳转页面。

需要把方法加上async和await。 警告: Promise returned from xxx is ignored 就没有了。

export async function post() {
    await axios.post("http://localhost:9090/checkFace").then(function (res) {
        if (res.data.code == 1) {
            ElMessageBox.alert('识别成功', '系统提示', {
                confirmButtonText: 'OK'
            })
            this.$router.push("/");
        } else {
            ElMessageBox.alert('识别失败', '系统提示', {
                confirmButtonText: 'OK'
            })
        }
    }).catch(function (err) {
        console.log(err)
    });
}

但是出现了新的问题,浏览器警告:无法识别$router

然后我想了想,这虽然是在vue脚手架中,但是是js文件,this指向的应该不是vue对象,在js文件中this指向应该是window。而window中是没有$router的,所以警告无法识别$router。

解决:

在js文件中引入router。

import router from "@/router";
export async function post() {
    await axios.post("http://localhost:9090/checkFace").then(function (res) {
        if (res.data.code == 1) {
            ElMessageBox.alert('识别成功', '系统提示', {
                confirmButtonText: 'OK'
            })
            router.push("/");
        } else {
            ElMessageBox.alert('识别失败', '系统提示', {
                confirmButtonText: 'OK'
            })
        }
    }).catch(function (err) {
        console.log(err)
    });
}

OK 。 搞定。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
Vue3中出现"TypeError: Cannot read properties of undefined (reading '$router')"错误通常是因为在访问`$router`属性时,`$router`对象未定义。这可能是由于以下几个原因导致的: 1. 组件未正确引入`vue-router`:请确保在使用`$router`之前,已经正确引入了`vue-router`并将其安装到Vue实例中。 2. 组件未正确配置路由:请确保在路由配置中正确定义了路由,并将其与组件关联起来。确保在路由配置中使用了`component`选项来指定组件,并且在组件中正确访问`$router`属性。 3. 组件未正确放置在路由中:请确保组件被正确放置在路由中,以便能够访问到`$router`属性。如果组件被嵌套在其他组件中,请确保父组件也正确配置了路由。 以下是一个示例,演示了如何在Vue3中正确使用`$router`属性: ```javascript // main.js import { createApp } from 'vue' import App from './App.vue' import router from './router' createApp(App).use(router).mount('#app') // router.js import { createRouter, createWebHistory } from 'vue-router' import Home from './views/Home.vue' import About from './views/About.vue' const routes = [ { path: '/', component: Home }, { path: '/about', component: About } ] const router = createRouter({ history: createWebHistory(), routes }) export default router // App.vue <template> <div> <router-link to="/">Home</router-link> <router-link to="/about">About</router-link> <router-view></router-view> </div> </template> <script> export default { mounted() { console.log(this.$router) // 在这里可以正常访问到$router属性 } } </script> ```
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

NoBug.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值