尝试使用 .json 但 Vue-CLI 找不到该文件
Trying to consume a .json but Vue-CLI doesn't find the file
我有一个 SQL database
,我用 json_encode
获取了它。我已将结果复制到 .json
中以尝试使用 Vue-cli
来使用它。所以,我将文件放在 assets
,然后放在 components
,但在 console
,我总是收到这条消息:
GET http://localhost:8080/assets/fetch.json 404 (Not Found)
此外,这是我在 App.vue
中输入的代码
<script>
export default {
data() {
return { dm: [], error: [] };
},
mounted() {
this.$axios
.get("views/fetch.json")
.then(response => {
this.dm = response.data;
})
.catch(e => {
this.error.push(e);
});
}
};
</script>
并使用它:
<tr v-for="(dm, index) in dm" :key="index">
<td>{{ dm.id_dm }}</td>
</tr>
我不太确定你的项目结构是什么,但你是否尝试过直接使用 import
语句导入文件,而不是像这样使用 axios
:
<script>
import data from "@/views/fetch.json"
</script>
然后您可以将 mounted()
中的 data
分配给您可以循环访问的其他变量,例如。
我有一个 SQL database
,我用 json_encode
获取了它。我已将结果复制到 .json
中以尝试使用 Vue-cli
来使用它。所以,我将文件放在 assets
,然后放在 components
,但在 console
,我总是收到这条消息:
GET http://localhost:8080/assets/fetch.json 404 (Not Found)
此外,这是我在 App.vue
中输入的代码<script>
export default {
data() {
return { dm: [], error: [] };
},
mounted() {
this.$axios
.get("views/fetch.json")
.then(response => {
this.dm = response.data;
})
.catch(e => {
this.error.push(e);
});
}
};
</script>
并使用它:
<tr v-for="(dm, index) in dm" :key="index">
<td>{{ dm.id_dm }}</td>
</tr>
我不太确定你的项目结构是什么,但你是否尝试过直接使用 import
语句导入文件,而不是像这样使用 axios
:
<script>
import data from "@/views/fetch.json"
</script>
然后您可以将 mounted()
中的 data
分配给您可以循环访问的其他变量,例如。