Vue 中引入 json 的三种方法
Vue 中引入 json 的三种方法
json的定义:
JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式。
JSON 是 JS 对象的字符串表示法,它使用文本表示一个 JS 对象的信息,本质是一个字符串。
1.require-运行时加载
test.json文件
{ "testData": "hello world", "testArray": [1,2,3,4,5,6], "testObj": { "name": "tom", "age": 18 } }
// require引用时,放src和放statci都可以,建议放static const testJson = require('../../static/json/test.json'); console.log('testData', testData);
2.import-编译时输出接口
import testImportJson from '../../static/json/test.json' console.log('testData', testData);
3. 通过http请求获取
this.$http.get('http://localhost:8080/static/json/test.json').then(res => { console.log('testData', res.data); });
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· winform 绘制太阳,地球,月球 运作规律
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· AI 智能体引爆开源社区「GitHub 热点速览」
· Manus的开源复刻OpenManus初探
· 写一个简单的SQL生成工具