【问题标题】:show result from json with axios使用 axios 显示 json 的结果
【发布时间】:2018-07-25 22:31:27
【问题描述】:

您好,我正在尝试使用 vue.js 显示 json 文件结果,目标是结果将显示在值上。

这是我的代码:

    data () {
            axios.get('http://localhost:3000/statcard').then(response => {
            this.items = response.data;
            this.items.forEach(item => {
            this.statsCards.push({ type: 'warning', icon: 'ti-server', title: 'Cases', value: item, footerText: 'Updated now', footerIcon: 'ti-reload' } );
            })
          })  

          return {

            statsCards: [
              {
                type: 'warning',
                icon: 'ti-briefcase',
                title: 'Cases',
                value:  total_orders,
                Time1: '08:00',
                valuenow: '250',
                textValueNow: 'Now',
                footerText: 'Updated now',
                footerIcon: 'ti-reload'
              },
]

这是我的 json 文件的结果:

{"total_orders":389,"total_cases":1188,"total_cases_08":[{"numbers":"389","type":"orders","cron":"2018-02-15 08:00:19"}],"total_orders_08":[{"numbers":"1191","type":"Cases","cron":"2018-02-15 08:00:18"}]}

我试图在 stascards 上显示 total_orders":389 的价值

感谢您的帮助

【问题讨论】:

  • 如果 JSON 是您的响应,则代码在 forEach() 处出错(在非数组上尝试数组方法)。

标签: javascript json laravel vue.js axios


【解决方案1】:

如果我理解正确,这样做应该可以:

new Vue({
  el: "#app",

  data() {
      statsCards: []
  },
  mounted() {
    axios.get("http://localhost:3000/statcard").then(response => {
      this.statsCards.push({
        type: "warning",
        icon: "ti-server",
        title: "Cases",
        value: response.data.total_orders,
        footerText: "Updated now",
        footerIcon: "ti-reload"
      });
    });
  }
});

【讨论】:

  • 只有当你把它放在promise里面。
  • 确实,否则他需要处理异步请求或使用状态。
  • @velour 我有错误 Module build failed: SyntaxError: this is a reserved word (181:8) this.statsCards.push:
  • 查看更新的答案,现在可能没问题(我想与您的 Vue 实例代码非常相似)。
  • @velour 谢谢你... :-)
猜你喜欢
  • 2018-06-30
  • 2016-04-08
  • 2021-12-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-06-01
  • 1970-01-01
相关资源
最近更新 更多