JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式。 JSON 是 JS 对象的字符串表示法,它使用文本表示一个 JS 对象的信息,本质是一个字符串。
test.json文件
{
"testData": "hello world",
"testArray": [1,2,3,4,5,6],
"testObj": {
"name": "tom",
"age": 18
}
}
// require引用:
mounted() {
// require引用时,放src和放statci都可以,建议放static
const testJson = require('../../static/json/test.json');
// const testJson = require('./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);
// {
// "name": "tom",
// "age": 18
// }
}
test.json文件
{
"testData": "hello world",
"testArray": [1,2,3,4,5,6],
"testObj": {
"name": "tom",
"age": 18
}
}
// import 引用
// import引用时,放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;
console.log('testImportData',testData);
// ‘hello world’
console.log('testImportArray',testArray);
// [1,2,3,4,5,6]
console.log('testImportObj',testObj);
// {
// "name": "tom",
// "age": 18
// }
}
}
test.json文件
{
"testData": "hello world",
"testArray": [1,2,3,4,5,6],
"testObj": {
"name": "tom",
"age": 18
}
}
// http引用
methods:{
async jsonHttpTest(){
const res = await this.$http.get('http://localhost:8080/static/json/test.json');
// 放在src中的文件都会被webpack根据依赖编译,无法作为路径使用,static中的文件才可以作为路径用
// const res = await this.$http.get('http://localhost:8080/src/page/json/test.json');
const {testData, testArray, testObj} = res.data;
console.log('testHttpData',testData);
// ‘hello world’
console.log('testHttpArray',testArray);
// [1,2,3,4,5,6]
console.log('testHttpObj',testObj);
// {
// "name": "tom",
// "age": 18
// }
}
},
mounted() {
this.jsonHttpTest();
},
本文系转载,前往查看
如有侵权,请联系 cloudcommunity@tencent.com 删除。
本文系转载,前往查看
如有侵权,请联系 cloudcommunity@tencent.com 删除。