Alv*_*vin 2 javascript vue.js axios vuex
我正在尝试使用 Axios 将所有数据从 API 返回到我的视图,并使用 Vuex 来存储我的状态。此源代码可以返回'''console.log''',但无法传递给查看。
我曾尝试将 mount() 更改为 change() 但它仍然无法正常工作
这是我的源代码:在 './store/modules/form.js' 中:
import Vue from "vue";
import axios from "axios";
import VueAxios from "vue-axios";
Vue.use(VueAxios, axios);
const state = {
posts: [],
};
const mutations = {
setPosts(state, posts) {
state.posts = posts;
}
};
const actions = {
loadPosts({ commit }) {
axios
.get("https://jsonplaceholder.typicode.com/posts")
.then(response => resnponse.data)
.then(posts => {
commit("setPosts", posts);
console.log(posts);
});
}
};
export default {
//namespaced: true,
state,
mutations,
actions
};
Run Code Online (Sandbox Code Playgroud)
在 './store/index.js 中:
import Vuex from "vuex";
import Vue from "vue";
import form from "./modules/form";
import axios from "axios";
import VueAxios from "vue-axios";
Vue.use(VueAxios, axios);
Vue.use(Vuex);
export default new Vuex.Store({
modules: {
form
}
});
Run Code Online (Sandbox Code Playgroud)
在 'components/Posts.vue' 中:
<template>
<div>
<div class="container">
<table class="table table-striped">
<thead>
<tr>
<th>ID</th>
<th>Title</th>
<th>Body</th>
</tr>
</thead>
<tbody>
<tr v-for="post in posts" :key="post.id">
<td>{{ post.id }}</td>
<td>{{ post.title }}</td>
<td>{{ post.body }}</td>
</tr>
</tbody>
</table>
</div>
</div>
</template>
<script>
import { mapState } from "vuex";
export default {
name: "PostsPage",
computed: mapState(["posts"]),
mounted() {
this.$store.dispatch("loadPosts");
},
};
</script>
Run Code Online (Sandbox Code Playgroud)
如果你能帮助我,非常感谢。
我开始认为既然他在使用modules他应该尝试
computed: mapState("form", ["posts"]),
Run Code Online (Sandbox Code Playgroud)
(我的代表太低,无法添加评论:()
| 归档时间: |
|
| 查看次数: |
6403 次 |
| 最近记录: |