首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Vue - print数组

Vue - print数组
EN

Stack Overflow用户
提问于 2018-03-07 20:32:45
回答 2查看 11.6K关注 0票数 3

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

有什么办法可以做到吗?

代码语言:javascript
复制
    {
  "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": {}
    }
  ]
}

代码语言:javascript
复制
<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>

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-03-07 20:37:14

您需要遍历errorMessages数组

代码语言:javascript
复制
<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(", ") => 'hola, mundo')

代码语言:javascript
复制
<span v-if="file.error">{{ file.response.errorMessages.join(", ") }}</span>
票数 5
EN

Stack Overflow用户

发布于 2021-01-23 18:21:46

代码语言:javascript
复制
    {
  "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": {}
    }
  ]
}

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49152074

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档