vue循环读取json文件
时间: 2023-10-26 19:01:29 浏览: 94
要在Vue中循环读取JSON文件,你可以使用Ajax请求或者直接导入JSON文件。以下是两种实现方式的示例代码:
方法一:使用Ajax请求
```javascript
<template>
<div>
<div class="menu-bar" v-for="title in titles" :key="title.name">
<a :href="title.url" target="rightFrame">› {{ title.name }}</a>
</div>
</div>
</template>
<script>
import axios from 'axios';
export default {
data() {
return {
titles: []
};
},
created() {
axios.get('your_json_file.json')
.then(response => {
this.titles = response.data.titles;
})
.catch(error => {
console.log(error);
});
}
};
</script>
```
方法二:直接导入JSON文件
```javascript
<template>
<div>
<div class="menu-bar" v-for="title in titles" :key="title.name">
<a :href="title.url" target="rightFrame">› {{ title.name }}</a>
</div>
</div>
</template>
<script>
import json from './your_json_file.json';
export default {
data() {
return {
titles: json.titles
};
}
};
</script>
```
这两种方法都可以循环读取JSON文件,并将其中的标题和链接展示在Vue组件中。你可以根据具体的需求选择其中一种方法来实现。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* *3* [用vue循环遍历来自json的数据,并实现到标签中](https://blog.csdn.net/sonteki/article/details/114045711)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"]
- *2* [Vue中怎样获取json文件中的数据](https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/84946744)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"]
[ .reference_list ]
阅读全文