【问题标题】:How can I able to display "No data" if there is no data in the json response in vue js?如果 vue js 中的 json 响应中没有数据,如何显示“无数据”?
【发布时间】:2018-05-07 04:45:07
【问题描述】:

当没有数据时,什么都不显示。当数据为空时,任何人都可以帮我打印一条消息为“无搜索结果”。我是第一次做工作,只有基本的想法。请帮我解决这个问题?

数据为空时我的json数据是:

{ "status": true, "data": [] }
searchContent = new Vue({
  el: "#searchContent",
  data: {
    vector: {}
  }
});

categories = new Vue({
  el: '#categories',
  data: {
    category: 0,
    subcategory: 0,
    content: false
  },
  watch: {
    subcategory: function(e) {
      this.prefetch();
    },
    category: function(e) {
      this.prefetch();
    },
  },
  methods: {
    prefetch: function() {
      var filter = {};
      filter['category'] = this.category;
      filter['subcategory'] = this.subcategory;
      if (this.content !== false)
        this.content.abort()
      this.content = $.ajax({
        'url': 'filter/',
        data: filter,
        dataType: "JSON",
        type: "POST",
        success: function(e) {
          window.searchContent.vector = e.data;
          console.log(e);
        }
      })
    }
  }
})
<div id="searchContent">
  <div v-for="row in vector">{{row.bussinessName}}</div>
  <div>

【问题讨论】:

    标签: javascript jquery vue.js vue-component


    【解决方案1】:

    您应该在标记中完全做到这一点。试试这个...

    <div id="searchContent">
      <div v-for="row in vector">{{row.bussinessName}}</div>
      <div v-if="vector.length === 0">No search results</div>
    <div>
    

    【讨论】:

      【解决方案2】:

      检查数据长度成功。

      success: function(e) {
             if(e.data.length > 0)
                window.searchContent.vector = e.data;
               else
                 window.searchContent.vector= 'No search reults'
              }
      

      【讨论】:

      • 一些空值正在打印
      • console.log(e.data.length) 检查数据为空时的长度。
      • 0来了/..如果没有数据我需要显示图像
      猜你喜欢
      • 1970-01-01
      • 2018-03-23
      • 2017-11-30
      • 2019-07-22
      • 2018-12-28
      • 2020-02-19
      • 2018-04-02
      • 2019-08-13
      • 1970-01-01
      相关资源
      最近更新 更多