vue.js - 如何在 vuejs 中使用 axios 访问 json 文件?
问题描述
我的 data.json 中有以下内容存储在公用文件夹中:
[
{
"content1": "testing"
}
]
在我的 Vuex 商店中,我有以下状态和突变:
state: {
contents: []
},
mutations: {
getContent1(state){
axios
.get("data.json")
.then(response => (state.contents = response.data))
}
在我的组件中,我可以使用以下命令通过 ...mapState 查看我的文件:
<p> {{ contents }} </p>
但是,如果我要切换到,{{ contents.content1 }}
我将无法再看到屏幕上的数据。关于发生了什么的任何提示?
其次,我希望使用 axios 更改 json 文件中的“content1”数据,我打算这样写:
putContent1(state) {
axios
.put("data.json")
.then(response => state.contents = state.newContents))
// however, I am not quire sure the last line on the .then, on whether that is correct or not.
任何帮助,将不胜感激!
解决方案
推荐阅读
- r - 如何根据日期是否在范围内分配级别?
- python - 使用 Single Pass 算法将通过 for 循环从数据帧中生成的列表与字典值相交的快速方法
- javascript - 运行存储在字符串变量中的 Javascript 函数
- android - 模拟相机意图结果时出现 FileNotFoundException
- spring - Spring,从不同的控制器访问方法
- angular - 将值传递给子组件Angular 7
- java - 不同的信号 11 (SIGSEGV),代码 1 (SEGV_MAPERR) 崩溃
- google-colaboratory - 是否可以在 Google Colab 上使用 OpenSlide?
- python - 什么类型的选项适用于 SlugField?
- javascript - 使用 OpenLayers 添加简单的标记/点