如何使用 Vue.js 读取 JSON 文件
在 Vue.js 中读取 JSON 文件主要有两种方法:使用 HTTP 请求或直接使用本地文件。
使用 HTTP 请求
要使用 HTTP 请求读取 JSON 文件,可以使用 axios
库:
<code class="javascript">import axios from \'axios\' export default { methods: { readJSON() { axios.get(\'path/to/json/file.json\') .then((response) => { // 处理读取到的 JSON 数据 }) .catch((error) => { // 处理错误 }) } } }