faker一直显示同一张图,如何避免?

Faker shows the same picture all the time, how to avoid it?

我正在使用 faker 来制作随机对象数组,如下所示:

{
  "image":  faker.random.arrayElement([
    faker.image.nature(), 
    faker.image.city(), 
    faker.image.food() 
  ]),
  "price": faker.random.number({ min: 20, max: 300 }),
  "beds": faker.random.number({ min: 1, max: 15 }),
  "type": faker.random.arrayElement([ 
    "Entire home", 
    "Private room", 
    "Shared room" 
  ])
}

实际上当像这样制作 12 个元素时会发生什么 - 所有数据都是随机的但不是图像,这在每次加载中都是相同的。

每次刷新都有其他照片,但数组中的所有元素都包含相同的图像。

我该怎么办?

准确地说,faker.js 返回相同的 url,例如http://lorempixel.com/640/480/nature 用于 faker.image.nature() 调用,因为 lorempixel.com 服务根据请求 returns 随机图像。

如果您更愿意看到完全相同的图像,那么这可能与您的浏览器缓存响应有关(如果是这样,请尝试在开发过程中禁用缓存或向图像添加随机查询字符串,例如 'image': `${faker.image.nature()}?random=${Date.now()}`).

所以回答你的问题 - 你不需要做任何事情。您最终会获得随机图像(根据 lorempixel.com 的要求)。

寻找相同的。在文档中找到这个。

faker.image.nature(width, height, randomize)

参数 |名称 |类型 |默认 | |------------|--------|--------| |宽度? |编号| 640 | |高度? |编号| 480 | |随机化? |布尔值|假 |

来源:https://fakerjs.dev/api/image.html#nature