Vue中引入json的三種方式總結(jié)
vue引入json的三種方式
json的定義
JSON(JavaScript Object Notation) 是一種輕量級(jí)的數(shù)據(jù)交換格式。
JSON 是 JS 對(duì)象的字符串表示法,它使用文本表示一個(gè) JS 對(duì)象的信息,本質(zhì)是一個(gè)字符串。
1.require-運(yùn)行時(shí)加載
test.json文件
{
? "testData": "hello world",
? "testArray": [1,2,3,4,5,6],
? "testObj": {
? ? "name": "tom",
? ? "age": 18
? }
}// require引用:
mounted() {
? ? // require引用時(shí),放src和放statci都可以,建議放static
? ? const testJson = require('../../static/json/test.json');
? ? const {testData, testArray, testObj} = testJson;
? ? console.log('testData',testData);
? ? // ‘hello world'
? ? console.log('testArray',testArray);
? ? // [1,2,3,4,5,6]
? ? console.log('testObj',testObj);
}2.import-編譯時(shí)輸出接口
// import 引用
// import引用時(shí),放src和放statci都可以,建議放static
import testImportJson from '../../static/json/test.json'
// import testImportJson from './json/test.json'
export default {
? data(){
? ? return{
? ? ? testImportJson ?
? ? }
? },
? mounted() {
? ? const {testData, testArray, testObj} = this.testImportJson;
? }
}3. 通過(guò)http請(qǐng)求獲取
// http引用
methods:{
? async jsonHttpTest(){
? ? const res = await this.$http.get('http://localhost:8080/static/json/test.json');
? ? // 放在src中的文件都會(huì)被webpack根據(jù)依賴編譯,無(wú)法作為路徑使用,static中的文件才可以作為路徑用
? ? const {testData, testArray, testObj} = res.data;
? }
},
mounted() {
? this.jsonHttpTest();
},vue解析json數(shù)據(jù)
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<title></title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<script src="https://cdn.bootcss.com/vue/2.2.2/vue.min.js"></script><!--vue.min.js的庫(kù)-->
<script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script><!--jQuery庫(kù)-->
</head>
<body>
<div id="main">
<div v-for="item in rows">
<p>姓名:{{item.name}}</p>
<p>昵稱:{{item.nick}}</p>
</div>
</div>
</body>
<script>
$(document).ready(function () {
$.getJSON("data.json", function (result, status) {
var v = new Vue({
el: '#main',
data: {
rows: result
}
})
});
});
</script>
</html>test.json
[
{
"name": "王小婷",
"nick": "祈澈菇?jīng)?
}, {
"name": "安安",
"nick": "壞兔子"
}, {
"name": "編程微刊",
"nick": "簡(jiǎn)書"
}
]總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
vue+element樹組件 實(shí)現(xiàn)樹懶加載的過(guò)程詳解
這篇文章主要介紹了vue+element樹組件 實(shí)現(xiàn)樹懶加載的過(guò)程,本文通過(guò)圖文實(shí)例代碼相結(jié)合給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-10-10
利用Vue實(shí)現(xiàn)簡(jiǎn)易播放器的完整代碼
這篇文章主要給大家介紹了關(guān)于如何利用Vue實(shí)現(xiàn)簡(jiǎn)易播放器的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12
前端項(xiàng)目vue3/React如何使用pako庫(kù)解壓縮后端返回gzip數(shù)據(jù)
pako是一個(gè)流行的JS庫(kù),用于在瀏覽器中進(jìn)行數(shù)據(jù)壓縮和解壓縮操作,它提供了對(duì)常見的壓縮算法的實(shí)現(xiàn),使開發(fā)者能夠在客戶端上輕松進(jìn)行數(shù)據(jù)壓縮和解壓縮,這篇文章主要介紹了前端項(xiàng)目vue3/React使用pako庫(kù)解壓縮后端返回gzip數(shù)據(jù),需要的朋友可以參考下2024-07-07
VUE實(shí)現(xiàn)一個(gè)Flappy Bird游戲的示例代碼
這篇文章主要介紹了VUE實(shí)現(xiàn)一個(gè)Flappy Bird的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04
探索Vue.js component內(nèi)容實(shí)現(xiàn)
這篇文章主要和大家一起探索Vue.js component內(nèi)容實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-11-11

