Vue中如何獲取json文件中的數(shù)據(jù)
場景
訪問百度音樂API需要傳遞音樂類型參數(shù),而這些參數(shù)是存在musictype.json中,
現(xiàn)在在組件listcate.vue需要獲取json數(shù)據(jù)。
json文件內容:

文件位置:

實現(xiàn)
musictype.json
{
"currentType":[1,2,11,21,22,23,24,25]
}listcate.vue
<template lang="html">
<div>
<ListCate_List v-for="item in musicTypeJSON" :musicType="item" />
</div>
</template>
<script>
import MusicType from "../assets/data/musictype.json"
import ListCate_List from "../components/ListCate_List"
export default {
data(){
return{
musicTypeJSON:[]
}
},
components:{
ListCate_List
},
created(){
this.musicTypeJSON = MusicType.currentType
}
}
</script>
<style lang="css">
</style>注:
是通過import MusicType from "../assets/data/musictype.json" 引入的
然后通過 this.musicTypeJSON = MusicType.currentType 賦值給musicTypeJSON,然后通過
<ListCate_List v-for="item in musicTypeJSON" :musicType="item" />
循環(huán)遍歷取值。
然后再ListCast_List.vue中直接通過:
props:{
musicType:{
type:[String,Number],
default:1
}
},
mounted(){
const ListCateUrl = this.HOST + "/v1/restserver/ting?method=baidu.ting.billboard.billList&type="+ this.musicType +"&size=3&offset=0"
this.$axios.get(ListCateUrl)
.then(res => {
console.log(res.data)
this.listCateData = res.data
})
.catch(error => {
console.log(error);
})
}
}獲取并使用。
到此這篇關于Vue中如何獲取json文件中的數(shù)據(jù)的文章就介紹到這了,更多相關vue獲取json文件數(shù)據(jù)內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
vue使用axios實現(xiàn)動態(tài)追加數(shù)據(jù)
在vuejs中使用axios時,有時候需要追加數(shù)據(jù),比如,移動端下拉觸底加載,分頁加載,滑動滾動條等,下面小編就來為大家介紹一下如何使用使用axios實現(xiàn)動態(tài)追加數(shù)據(jù)吧2023-10-10
Vue router的addRoute方法實現(xiàn)控制權限方法詳解
這篇文章主要介紹了vue動態(tài)路由添加,vue-router的addRoute方法實現(xiàn)權限控制流程,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習吧2022-09-09
在Vue3和TypeScript中大文件分片上傳的實現(xiàn)與優(yōu)化
本文介紹在 Vue 3 和 TypeScript 環(huán)境下大文件分片上傳的實現(xiàn)與優(yōu)化,包括項目前后端技術棧,前端的文件切片、并發(fā)上傳、計算 Hash、斷點續(xù)傳和用戶體驗優(yōu)化,后端的文件接收存儲、切片合并、異常處理與日志記錄,還提及遇到的問題及解決方案,總結了此方式的優(yōu)勢和重要性2025-01-01
解決vue語法會有延遲加載顯現(xiàn){{xxx}}的問題
今天小編就為大家分享一篇解決vue語法會有延遲加載顯現(xiàn){{xxx}}的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-11-11

