javascript - Vue js 问题从 API 调用 url
问题描述
我正在尝试从 API 显示商店 URL。API 中有不同的 URL 可用,在输出中,它们显示为一个链接。如何在不同的行中显示 URL?
我的Vue代码:
<a v-bind:href=“storeUrl”>
{{storeUrl}}
</a>
我的脚本:
....
computed:{
storeUrl() {
return this.games.store.map(({{url}}) => url).join(‘ ’)
},
}
我正在使用https://api.rawg.io/api/games API
解决方案
用实际例子更新
首先,不要将它们加入计算中,然后使用 v-for 实现,这样的东西应该可以工作。
基本上这当然是我自己的想法,但是基于实际的 API 数据,这样的事情应该可以工作,在一个循环中循环,我绘制数据只是为了便于使用,你最终会得到类似的东西:
[
{
key: 'gta-v',
storeUrls: [
{
key: 'steam',
url: 'http:// ...'
},
{
key: 'riot',
url: 'http:// ...'
}
]
},
{
key: 'fortnite',
storeUrls: [
{
key: 'steam',
url: 'http:// ...'
},
{
key: 'riot',
url: 'http:// ...'
}
]
}
]
使用它,我们还可以在模板中加倍使用 v-for,并按游戏对数据进行排序,并为每个游戏循环通过它的 storeUrl 以获得一个漂亮的干净列表,这也利用了实际键的使用,而不是索引。
<template>
<div class="root">
<div class="game" v-for="game in games" :key="game.key">
<h1>{{ game.key }}</h1>
<a v-for="store in game.storeUrls" :href=“store.url” :key="store.key">
{{store.url}}
</a>
</div>
</div>
</template>
export default {
data() {
return {
myData: null
}
},
computed: {
games() {
if (!this.myData) return [];
return this.myData.results.map(game => {
key: game.slug,
storeUrls: game.stores.map(store => {
return {
key: store.store.slug,
url: store.url_en
}
});
});
}
},
methods: {
getData() {
// However you do it, but the base data, with no mapping.
this.myData = axios.get('...');
}
}
}
推荐阅读
- mysql - SQL 查询:奇怪的 SUM 行为
- node.js - nodeJS和expressJS中的Bootstrap问题
- ruby-on-rails - 如何替换rubyracer javascript运行时
- python - 在 boto 中将 python 函数转换为 AWS Lambda 处理程序
- python - 在 pandas for python 中使用布尔过滤时选择要显示的列
- arangodb - 计算两个节点之间的跳数并识别层次图中最低节点的方法
- airflow - 如何阻止 Airflow 记录连接字符串?
- javascript - 正则表达式防止同一字符连续出现超过 3 次
- r - 根据另一个 txt 文件对数据框列进行排序
- list - Dart list.take - 错误:输入“SubListIterable”
' 不是 List 类型的子类型