Javascript 将GET对象推送到数据返回中的空对象

Javascript 将GET对象推送到数据返回中的空对象,javascript,vue.js,vuejs2,Javascript,Vue.js,Vuejs2,我有以下vue应用程序: new Vue({ name: 'o365-edit-modal-wrapper', el: '#o365-modal-edit-wrapper', data: function() { return { list: {}, } }, created() { this.get_array_of_post_objects(this.list, 'applicat

我有以下vue应用程序:

new Vue({
    name: 'o365-edit-modal-wrapper',
    el: '#o365-modal-edit-wrapper',
    data: function() {
        return {
            list: {},
        }
    },
    created() {
        this.get_array_of_post_objects(this.list, 'applications');
    },
    methods: {
        get_array_of_post_objects(list, slug) {
            wp.api.loadPromise.done(function () {
                const Posts = wp.api.models.Post.extend({
                    url: wpApiSettings.root + 'fh/v1/menus/' + slug,
                });
                const all_posts = new Posts();
                all_posts.fetch().then(function(posts) {

                    console.log(posts.data);

                });
            });
        },
    },
});
console.log(posts.data)
输出以下项目:

我如何将该对象分配给我的空
列表:{}
,以便在我的模板中,我可以按如下方式提取它:

<button class="button" v-for="app in list.selected" :key="app.order">


所有的帮助将不胜感激!每次尝试推送未定义的项时,我都会将其返回。

每当我必须使用JS时,我总是更喜欢
ES6
语法。 让我为您重新编写代码,并尝试它是否正常工作。我更喜欢在函数中使用箭头函数

newvue({
名称:“o365编辑模式包装器”,
el:#o365模态编辑包装器“,
数据(){
返回{
列表:{},
};
},
创建(){
这个.get_-array_的_-post_对象(“应用程序”);
},
方法:{
获取\u post\u对象的\u数组\u(slug){
wp.api.loadPromise.done(()=>{
const Posts=wp.api.models.Post.extend({
url:wpApiSettings.root+“fh/v1/menus/”+slug,
});
const all_posts=新posts();
所有_posts.fetch().然后((posts)=>{
console.log(posts.data);
this.list=posts.data;
});
});
},
},
});

希拉格有答案。但是,我不建议使用箭头函数,因为IE不支持它,所以我只需要创建一个单独的函数来处理这项工作。这使代码更具可读性。因此,类似这样的方法应该有效:

new Vue({
  name: "o365-edit-modal-wrapper",
  el: "#o365-modal-edit-wrapper",
  data() {
    return {
      list: {},
    };
  },
  created() {
    this.get_array_of_post_objects("applications");
  },
  methods: {
    get_array_of_post_objects(slug) {
      wp.api.loadPromise.done(function() {
        const Posts = wp.api.models.Post.extend({
          url: wpApiSettings.root + "fh/v1/menus/" + slug,
        });
        const all_posts = new Posts();
        all_posts.fetch().then(handlePostSuccess);
    },
    handlePostSuccess(post){
      console.log(posts.data);
      this.list = posts.data;
    }
  },
});

我还要补充一点,将es6与箭头函数一起使用时,可以单独使用
这个
的上下文,这在使用vue时是最好的。更容易分配,无需绑定。感谢大家的输入!看起来效果不错,但似乎vue draggable无法处理具有多个数组的单个对象
get_array_of_post_objects
函数的开头,这样您就可以调用
self.list=posts.data在末尾