【问题标题】:Vue.js - Return array from axiosVue.js - 从 axios 返回数组
【发布时间】:2019-06-07 20:46:49
【问题描述】:

我正在尝试从 axios 调用中获取一个数组:

以便我可以访问组件的数据。我知道我可以使用像

这样的东西
  return {
    a: []
  }
}

getTags(index) {
  axios.get('http://localhost:8080/user/tag?imageIndex=' + index)
    .then(response => {
      this.a = response.data
     })
}, 

但问题是,我为每个图像都有一个数组,并且图像的数量是动态的。所以我想只返回一个数组

有机会做我想做的事吗? 如果有一种动态的方法,我可以忍受在 data() 中生成所有数组。或者axios可以退货吗?

这里我的代码不起作用:

<template>
    <div id="SingleFile">
        <button
                id="refreshbtn"
                class="btn btn-primary btn-margin"
                @click="updateFileList">
            refresh
        </button>

        <gallery :images="images" :index="index" @close="index = null"></gallery>
        <div
                :key="imageIndex"
                :style="{ backgroundImage: 'url(' + image + ')', width: '300px', height: '200px' }"
                @click="index = imageIndex"
                class="image"
                v-for="(image, imageIndex) in images"

        >
            <div>
            <vue-tags-input
                    v-model="tag"
                    :tags="getTags(imageIndex)"
                    @tags-changed="newTags => tags = newTags"
            />
            </div>
        </div>
    <div class="upload">
        <upload-image url="http://localhost:8080/user" name="files" max_files="100"></upload-image>
    </div>
    </div>
</template>

<script>
    import VueGallery from 'vue-gallery';
    import axios from 'axios';
    import auth from './../service/AuthService'
    import router from './../router'
    import UploadImage from 'vue-upload-image';
    import VueTagsInput from '@johmun/vue-tags-input';

    export default {
    components: {
        'gallery': VueGallery,
        'upload-image': UploadImage,
        VueTagsInput
    },

    data() {
        return {
            images: [],
            index: null,
            tag: '',
        };
    },

    created() {
        this.checkAuth()
    },

    methods: {
        checkAuth() {
            if (auth.isAuthenticated()) {
                this.updateFileList()
            } else {
                router.replace('/')
            }
        },

        updateFileList() {
            axios.get('http://localhost:8080/user')
             .then(response => {
                 this.images = response.data
             })
        },

        getTags(index) {
            return axios.get('http://localhost:8080/user/tag?imageIndex=' + index)
                .then(response => {
                    return response.data
                })
        },
    },
};
</script>

【问题讨论】:

    标签: javascript vue.js vuejs2 axios


    【解决方案1】:

    最好的方法是在挂载的钩子中使用 axios 或在触发某些事件后调用方法来返回数据:

     mounted(){
        //return all your images using valid url
         axios.get('http://localhost:8080/user/tag')
           .then(response => {
              this.a = response.data
           })
       }
    

    你的方法应该是这样的:

    methods:{
      getTags(i){
       return this.a[i];
       }
    }
    

    【讨论】:

    • 我现在从后端返回一个 List[],现在我可以像描述的那样访问它。非常感谢!
    猜你喜欢
    • 2021-02-05
    • 1970-01-01
    • 2017-08-07
    • 2018-02-16
    • 2020-07-07
    • 2019-04-06
    • 2018-08-05
    • 2021-11-10
    • 2021-01-08
    相关资源
    最近更新 更多