类型错误:无法读取未定义的属性(读取“$router”)vuejs

iSn*_*uff 8 javascript vue.js vue-router vuejs2

因此,如果 api 调用返回状态 422,我尝试将用户重定向到不同的路线。但我收到错误

TypeError: Cannot read properties of undefined (reading '$router')
Run Code Online (Sandbox Code Playgroud)

我的routes.js:

{
        path: '/dashboard',
        component: Dashboard,
        name: 'Dashboard',
        beforeEnter: (to, form, next) =>{
            axios.get('/api/authenticated')
                .then(()=>{
                    next();
                }).catch(()=>{
                    return next({ name: 'Login'})
            })
        },
        children: [
            {
                path: 'documentCollections',
                component: DocumentCollection,
                name: 'DocumentCollections'
            },
            {
                path: 'document',
                component: Document,
                name: 'Document'
            },
            {
                path: 'createDocument',
                component: CreateDocument,
                name: 'CreateDocument'
            },
            {
                path: 'suppliers',
                component: Suppliers,
                name: 'Suppliers'
            },
            {
                path: 'settings',
                component: Settings,
                name: 'Settings'
            },
        ]
}
Run Code Online (Sandbox Code Playgroud)

我也有登录/注册组件,当我使用时

this.$router.push({ name: "DocumentCollections"});
Run Code Online (Sandbox Code Playgroud)

它重定向用户,没有任何错误。问题是当我处于仪表板组件的子组件中时。

在 documentCollections 组件中我有一个方法:

    loadCollections(){
        axios.get('/api/documentCollections')
            .then((response) => {
                this.Collections = response.data.data
                this.disableButtons(response.data.data);
            })
            .catch(function (error){
                if(error.response.status === 422){
                    //here is where the error happens
                    this.$router.push({ name: "Settings"});
                }
            });
    },
Run Code Online (Sandbox Code Playgroud)

这会加载集合,但如果用户有一些数据集 null api 返回状态 422。我希望他被重定向到设置组件。(documentCollection和Settings都是Dashboard的子组件)

为什么 this.$router.push 在这里不起作用,但在登录/注册组件中起作用?

Tam*_*oke 12

this在回调函数内部调用会创建一个新的this对象绑定,而不是常规函数表达式中的 Vue 对象。

您可以使用箭头语法来定义函数,因此this不会被覆盖。

.catch((error) => {
    if(error.response.status === 422){
        this.$router.push({name: "Settings"});
    }
})
Run Code Online (Sandbox Code Playgroud)

更多信息

另外一个选择

在 axios 调用之前定义另一个实例this,并在收到响应后使用它。

let self = this
...
self.$router.push({name: "Settings"})
Run Code Online (Sandbox Code Playgroud)

用你的代码

loadCollections(){
    let self = this;
    axios.get('/api/documentCollections')
        .then((response) => {
            this.Collections = response.data.data
            this.disableButtons(response.data.data);
        })
        .catch(function (error){
            if(error.response.status === 422){
                self.$router.push({name: "Settings"});
            }
        });
},
Run Code Online (Sandbox Code Playgroud)


小智 8

如果使用组合 API:

// Import
import router from "@/router";

//and use
router.push({name: "Settings"});
Run Code Online (Sandbox Code Playgroud)

  • 正如目前所写的,您的答案尚不清楚。请[编辑]添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。您可以[在帮助中心](/help/how-to-answer)找到有关如何写出好的答案的更多信息。 (2认同)