我有一个 Angular 应用程序,我想创建 Blackjack。为此,我将所有扑克牌的图像复制到 /assets
目录中。
我想循环遍历文件夹中的图像以将图像(或源网址)添加到数组中,但就像下面的答案一样,这在前端是不可能的。 Loop through all files/images in a folder with Angular
但是,我应该如何将它们添加到数组中呢?需要修改后台吗?
最佳答案
这是一副纸牌。您不需要在后端查询它们是什么,因为它们永远不会改变。只需使用文件名模式在前端生成数组即可。
function cardSet(rank) {
return Array(13).fill(0).map((x, indx) => `card-${rank}-${indx + 1}.png`);
}
const cards = [
...cardSet('club'),
...cardSet('diamond'),
...cardSet('heart'),
...cardSet('spade')
];
console.log(cards);
关于arrays - 如何使用 Angular 获取所有扑克牌图像并将其添加到数组中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56931076/