【问题标题】:BeforeRouteEnter not loading data to variableBeforeRouteEnter 未将数据加载到变量
【发布时间】:2021-07-05 20:17:30
【问题描述】:

我已经设置了一个可以正确发送数据的 Web API,但是,我的 vuejs 应用程序有一个数据表组件,我使用 BeforeRouteEnter 钩子与 Axios 进行 API 调用,但来自响应的数据不会保存到我的数据变量。

是不是像我的数组queryResult这样的数据变量在我的get请求之后才被加载,因此它无法保存到它?我还尝试将 Axios 代码和变量更新调用为一种方法,但由于某种原因它无法识别方法 queryDB,因此它不起作用,所以它再次让我相信我的时间已经关闭。

我的代码如下:

<template id="dashboard">
    <div>
        <v-card>
            <v-card-title>
                <v-text-field v-model="search"
                              append-icon="mdi-magnify"
                              label="Search"
                              single-line
                              hide-details></v-text-field>
            </v-card-title>
            <v-data-table :headers="headers"
                          :items="queryResult"
                          :search="search"></v-data-table>
        </v-card>
    </div>
</template>
    const dashboard = Vue.component('dashboard',
        {
        template: '#dashboard',
            data() {
                return {
                    userID: 'SB',
                    password: 'Yellow',
                    search: '',
                    headers: [
                        { text: 'ID', align: 'start', filterable: true, value: 'ID'},
                        { text: 'USERNAME', value: 'USERNAME' },
                        { text: 'FIRST_NAME', value: 'FIRST_NAME' },
                        { text: 'LAST_NAME', value: 'LAST_NAME' },
                    ],
                    queryResult: [],
                }
            },
            beforeRouteEnter(to, from, next) {
                axios.get(window.location.origin + '/home/DapperDatatableLoad', {
                    params: {}
                })
                    .then(function (response) {
                        queryResult = response.data;
                    })
                    .catch(function (error) {
                        console.log(error);
                    });
                next()
            },
            methods: {
                queryDB: function () {
                    axios.get(window.location.origin + '/home/DapperDatatableLoad', {
                        params: {
                            
                        }
                    })
                        .then(function (response) {
                            queryResult = response.data;
                        })
                        .catch(function (error) {
                            console.log(error);
                        });
                }
            }
        });

【问题讨论】:

  • 如果您想等到数据加载完毕,请将next() 移入 then(并捕获)
  • @Suit Boy Apps 我已经将下一个移到里面,你说得对,它会延迟加载直到数据加载。但是,数据表仍然显示没有可用数据。我可以在 response.data 中看到一个 json 数组,但我认为它没有传递给我的数据部分中的 queryResult 变量。知道这是否是时间问题吗?
  • 我看到的另一个问题是 queryResult 是您的数据对象的一部分。所以 queryResult 应该在你的函数内部被称为this.queryResult
  • 您还应该检查控制台是否有错误,并将 vue 开发工具安装到浏览器中。通过这种方式,您可以实际可视化组件中的数据,然后从那里进行调试。例如:imgur.com/a/n8uwU2k

标签: vue.js vuejs2 vue-component vue-router


【解决方案1】:

我认为您的意思是在您的数据出现之前加载路由。

这是有道理的,因为axios.get 是异步的。这意味着当axios.get 向服务器请求数据时,主线程仍在执行代码。然后当axios.get 收到数据时,它会触发.then 回调。

在您的情况下,您的下一个命令是next(),它告诉路由器继续前进并在路由中前进。这意味着当axios.get 正在检索您已经调用过next() 的数据时。

如果你想先等待 axios 获取数据,你需要将 next() 移动到回调函数中。

beforeRouteEnter(to, from, next) {
    axios.get(window.location.origin + '/home/DapperDatatableLoad', {
        params: {}
    })
        .then(function (response) {
            next();
            this.queryResult = response.data; //Notice that `queryResult` is referred to with `this.`
        })
        .catch(function (error) {
            next();
            console.log(error);
        });
},

另一种解决方案是使用 await 语法

async beforeRouteEnter(to, from, next) {
    try{
        let response = await axios.get(window.location.origin + '/home/DapperDatatableLoad', {
            params: {}
        });
    } catch (error){
        console.log(error);
    }
    this.queryResult = response.data;       

    next();
},

【讨论】:

  • 这确实可以延迟加载,但我终其一生都无法弄清楚为什么它仍然说没有可用的数据。我在上面为 vuetify 数据表添加了组件标签。你看到任何明显的问题吗?就好像 queryResults 仍然等于 [] 尽管加载了数据。
  • 你的控制台输出是什么。或者你能创建一个codepen吗?
  • 抱歉耽搁了!控制台实际上显示 $attr 是只读错误,而 $listener 是只读错误。我决定尝试发出警报(JSON.stringify(queryResult));在 axios 调用之前以及在 next 之后查看 queryResult 数据中的变量此时是否存在并且它说它未定义。所以我认为这可能就是为什么永远不会分配数据的原因。我是否需要在 next 中执行 vm => {} 才能访问实例变量?
  • 我修复了 $attr 和 $listener 错误,因为这是由于第二个 vue cdn 链接造成的,但是当我尝试在 beforerouteenter 中将数据分配给它时,queryResult 变量仍然不存在。 >
  • 请创建一个代码沙盒或一个代码笔,以便我进一步研究。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-01-26
  • 2015-11-19
  • 2023-04-04
  • 2018-06-02
  • 1970-01-01
  • 1970-01-01
  • 2022-12-03
相关资源
最近更新 更多