vuex-未知的操作类型(无法调度我的操作)

时间:2019-03-26 13:37:24

标签: vue.js vuex vuex-modules

我试图将商店移动到单独的模块中,但无法弄清楚如何调度我的动作。我可以访问状态值,但不能访问操作。

请记住,我正在专门研究vue,所以我绝对是这里的初学者。

src / store / store.js(充当我在store文件夹中的索引)

import Vue from 'vue'
import Vuex from 'vuex'
import axios from 'axios'

import posts from './modules/posts'
import comments from './modules/comments'

Vue.use(Vuex)
axios.defaults.baseURL = 'https://someapi.com/'

export default new Vuex.Store({
    name: 'store',
    namespaced: true,
    modules: {
      posts,
      comments
    },
})

src / store / modules / posts.js

import Vue from 'vue'
import Vuex from 'vuex'
import axios from 'axios'

Vue.use(Vuex)
axios.defaults.baseURL = 'https://someapi.com/'

export default new Vuex.Store({
    name: 'posts',
    state: {
        posts: [],
        post: {}
    },
    mutations: {
        retrievePosts(state, posts){
            state.posts = posts
        },
        retrievePost(state, post){
            state.post = post
        },
    },
    actions: {
        retrievePosts (context) {
            axios.get('/blog/post/all')
                .then(response => {
                    var posts = response.data.data
                    context.commit('retrievePosts', posts)
                })
                .catch(error => {
                    console.log(error)
                })
            },
            retrievePost (context, slug) {
                axios.get('/blog/post/all')
                    .then(response => {
                        var post = response.data.data[1]
                        context.commit('retrievePost', post)
                    })
                    .catch(error => {
                        console.log(error)
                    })
            },
    }

})

src / components / BlogPostList.vue

<template>
    <div class="postList">
        <b-container v-for="post in this.$store.state.posts.posts" :key="post.id" class="post">
            <b-row>
                <b-col>
                    <h1><a :href="'post/' + post.slug">{{ post.title }}</a></h1>
                </b-col>
            </b-row>
            <b-row>
                <b-col cols="10">
                    <h4>{{ post.subtitle }}</h4>
                </b-col>
                <b-col>
                    <small>by {{ post.author_id }} </small>
                </b-col>
            </b-row>
            <b-row>
                <b-col>
                    <!-- <p v-html="post.body.substring(0, 1000) + '...'"></p> -->
                    <span class="post-text" v-html="post.body"/>
                </b-col>
            </b-row>
            <br />
            <b-row>
                <b-col>
                    <dx-button style="float:right"
                        text="read more"
                        @click="buttonClicked" 
                    />
                </b-col>
            </b-row>
            <hr>
        </b-container>
    </div>
</template>

<script>
import DxButton from "devextreme-vue/button";

export default  {
    components: {
        DxButton
    },
    name: 'BlogPostList',
    created () {
        this.$store.dispatch('retrievePosts')
    },
    methods: {
        buttonClicked: function() {
            alert("The Button was clicked");
        }
    }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped lang="scss">
.postList {
    h1 {
        font-size: 50px;
    }
    .post-text {
        text-align: justify;
    }
}
</style>

我尝试了发现的最“推荐”的修复程序,该修复程序正在更改

this.$store.dispatch('retrievePosts')

this.$store.dispatch('posts/retrievePosts')

但是我在控制台上遇到了同样的错误...

谢谢!

1 个答案:

答案 0 :(得分:1)

这可能是因为您在posts模块内创建了另一个Vuex存储,该存储已作为模块添加到主store.js中。

尝试将您的posts模块修改为以下内容。

import axios from 'axios';

axios.defaults.baseURL = 'https://someapi.com/';

export default {
  name: 'posts',
  state: {
    posts: [],
    post: {}
  },
  mutations: {
    retrievePosts(state, posts) {
      state.posts = posts;
    },
    retrievePost(state, post) {
      state.post = post;
    }
  },
  actions: {
    retrievePosts(context) {
      axios
        .get('/blog/post/all')
        .then((response) => {
          var posts = response.data.data;
          context.commit('retrievePosts', posts);
        })
        .catch((error) => {
          console.log(error);
        });
    },
    retrievePost(context, slug) {
      axios
        .get('/blog/post/all')
        .then((response) => {
          var post = response.data.data[1];
          context.commit('retrievePost', post);
        })
        .catch((error) => {
          console.log(error);
        });
    }
  }
};