我下面的Vue代码在页面加载时从数据库加载数据。我知道如何使用简单的Vue方法在HTML文件中显示它。但是,我想将其另存为Javascript数组。然后将Javascript数组输入地图(该地图需要Javascript数组)。我可以这样做吗?

var app = new Vue({
  el: '#purchase',
  mounted: function() {
    this.allPurchases()
  },
  data: {
    purchases: ""
  },
  methods: {
    allPurchases: function(){

      axios.get('http://localhost:3000/')
      .then(function (response) {
         console.log(response);
         app.purchases = response.data;
      })
      .catch(function (error) {
         console.log(error);
      });
    },
  },

});

最佳答案

您应将app.purchases替换为this.purchases

var app = new Vue({
  el: '#purchase',
  mounted: function() {
    this.allPurchases()
  },
  data() {
    return{
       purchases: []
      }

  },
  methods: {
    allPurchases: function(){

      axios.get('http://localhost:3000/')
      .then( (response) =>{
         console.log(response);
         this.purchases = response.data;
         window.purchases = response.data;//to be used globally
      })
      .catch(function (error) {
         console.log(error);
      });
    },
  },

});

关于javascript - 我可以将Vue输出保存为Javascript数组吗?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/57564149/

10-09 19:01