【问题标题】:Vue - print arrayVue - 打印数组
【发布时间】:2018-08-15 13:33:05
【问题描述】:

我有这个模型files,我有一个属性是response,里面有一个数组errorMessages,在我的Vue组件里面我想一个一个地显示错误,而不是数组格式。

有什么办法吗?

    {
  "files": [
    {
      "fileObject": true,
      "size": 9387,
      "name": "file_4444.pdf",
      "type": "application/pdf",
      "active": false,
      "error": true,
      "success": true,
      "postAction": "http://localhost:8000/api/v1/car/upload",
      "timeout": 0,
      "file": {},
      "el": {
        "__vue__": null
      },
      "response": {
        "uploadDone": 0,
        "uploadFail": 1,
        "errorMessages": [
          "User not found",
          "Car not found",
        ]
      },
      "progress": "100.00",
      "speed": 9591,
      "data": {},
      "headers": {},
      "id": "096vnj6rov9t",
      "xhr": {}
    }
  ]
}

<template>
  <div class="example-drag">
    <div class="upload">
      <ul v-if="files.length">
        <li v-for="(file, index) in files" :key="file.id">
          <span>{{file.name}}</span> -
          <span v-if="file.error"> {{ file.response.errorMessages }} <md-icon>thumb_down</md-icon> </span>
          <span v-else-if="file.success">success <md-icon>thumb_up</md-icon> </span>
          <span v-else-if="file.active">active <md-icon>thumb_up</md-icon> </span>
          <span v-else>  ... </span>

        </li>
      </ul>
   ...
</template>

【问题讨论】:

    标签: javascript json vue.js vuejs2 vue-component


    【解决方案1】:

        {
      "files": [
        {
          "fileObject": true,
          "size": 9387,
          "name": "file_4444.pdf",
          "type": "application/pdf",
          "active": false,
          "error": true,
          "success": true,
          "postAction": "http://localhost:8000/api/v1/car/upload",
          "timeout": 0,
          "file": {},
          "el": {
            "__vue__": null
          },
          "response": {
            "uploadDone": 0,
            "uploadFail": 1,
            "errorMessages": [
              "User not found",
              "Car not found",
            ]
          },
          "progress": "100.00",
          "speed": 9591,
          "data": {},
          "headers": {},
          "id": "096vnj6rov9t",
          "xhr": {}
        }
      ]
    }

    【讨论】:

      【解决方案2】:

      您需要遍历 errorMessages 数组

      <ul v-if="files.length">
        <li v-for="(file, index) in files" :key="file.id">
          <span>{{file.name}}</span> -
          <span v-if="file.error"> 
            <ol>
             <li v-for="err in file.response.errorMessages> {{ err }} </li>
            </ol>
          </span>
        </li>
      </ul>
      

      或者使用数组的.join方法(['hola', 'mundo'].join(", ") =&gt; 'hola, mundo'

      <span v-if="file.error">{{ file.response.errorMessages.join(", ") }}</span>
      

      【讨论】:

      • 谢谢,这正是我需要的。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-07-30
      • 1970-01-01
      • 1970-01-01
      • 2011-03-20
      • 2020-02-12
      相关资源
      最近更新 更多