简体   繁体   English

在Vue js中导入axios方法的正确语法

[英]Correct syntax for importing axios method in Vue js

I am trying to separate my axios calls from my main vue instance by importing them instead of calling them directly in the created hook. 我试图通过导入它们而不是直接在创建的钩子中调用它来将我的axios调用与我的主vue实例分开。

I have this in a separate file called data.js 我在一个名为data.js的单独文件中有这个

import axios from 'axios'
export default{
    myData() {
        return axios.get(`http://localhost:8080/data.json`)
            .then(response => {
                // JSON responses are automatically parsed.
                return response.data;
            })
            .catch(e => {
                return this.myErrors.push(e)
            });
},

And in my vue instance I have the following: 在我的vue实例中,我有以下内容:

import myDataApi from '@/api/data.js'

export default {
    name: 'app',
    components: {
        myDataApi, // not sure if this is correct
    },
    data: function () {
        return {
            myInfo: '',
        }
    },
    created() {
        this.myInfo = myDataApi.myData();
        console.log('this.myInfo= ', this.myInfo)
    },

I am trying to populate myInfo with the json called by myData. 我试图用myData调用的json填充myInfo。 This returns [object Promise] in Vue devtools and the as Promise {<pending>} in the console. 这将返回Vue devtools中的[object Promise]和控制台中的Promise {<pending>}

All the data I need is inside that Promise {<pending>} in an array called [[PromiseValue]]:Object so I know it is working, I just need to know the correct way implementing this. 我需要的所有数据都在一个名为[[PromiseValue]]:Object的数组中的Promise {<pending>}[[PromiseValue]]:Object所以我知道它正在工作,我只需要知道实现它的正确方法。

I don't have a development environment enabled to test this at the moment, but I do notice that you are trying to assign a variable the moment that the component is initialized. 我目前没有启用开发环境来测试它,但我注意到您正在尝试在组件初始化时分配变量。 This object is a promise, but you're not handling the promise after it is resolved inside the component where you have imported it. 这个对象是一个承诺,但是在您导入它的组件内部解析后,您不会处理它。

I would recommend trying to handle the promise inside of the actual component, something like: 我建议尝试处理实际组件内部的承诺,例如:

import myDataApi from '@/api/data.js'

export default {
    name: 'app',
    components: {
        myDataApi, // not sure if this is correct
    },
    data: function () {
        return {
            myInfo: '',
        }
    },
    created() {
        myDataApi.myData()
          .then((data) => {
            this.myInfo = data
            console.log('this.myInfo= ', this.myInfo);
          });
          .catch((e) => handleError) // however you want to handle it

    },

Just to add to @LexJacobs answer. 只是添加到@LexJacobs的答案。 I omitted the parenthesis around data in .then() as seen below. 我省略了.then()中数据的括号,如下所示。 Vue was squawking about data not being available even though it was. Vue正在咆哮着即使有数据也无法获得数据。 This solved that problem, although to be honest I don't know why. 这解决了这个问题,虽然老实说我不知道​​为什么。

myDataApi.myData()
    .then(data => {
        this.dataHasLoaded = true;
        this.myInfo = data;
    })
    .catch(e => {
        this.myErrors.push(e)
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM