简体   繁体   English

无法获取api数据并使用vuex存储-vue.js

[英]can't fetch api data and store with vuex - vue.js

I have problem about api fetch with vuex. 我对使用vuex获取api有问题。 And there is no problem with my endpoint. 而且我的端点没有问题。 I can see the json data. 我可以看到json数据。 But when I try to fetch it i can't store the data and displaying console error below. 但是,当我尝试获取它时,我无法存储数据并在下面显示控制台错误。

Error in mounted hook: "TypeError: _api_article_js__WEBPACK_IMPORTED_MODULE_0__.default.getArticles is not a function" 安装的挂钩中出现错误:“ TypeError:_api_article_js__WEBPACK_IMPORTED_MODULE_0 __。default.getArticles不是函数”

About my import and export: 关于我的进出口:

App.js App.js

window._ = require('lodash');

try {
    window.$ = window.jQuery = require('jquery');

    require('foundation-sites');
} catch (e) {}

window.axios = require('axios');

window.axios.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest';

let token = document.head.querySelector('meta[name="csrf-token"]');

if (token) {
    window.axios.defaults.headers.common['X-CSRF-TOKEN'] = token.content;
} else {
    console.error('CSRF token not found: https://laravel.com/docs/csrf#csrf-x-csrf-token');
}

import Vue from 'vue';
import router from './routes.js';
import store from './store.js';

new Vue({
     router,
     store,
}).$mount('#app')

config.js config.js

var api_url = 'mywebsite.com/api';

export const ESTATE_CONFIG = {
    API_URL: api_url,
}

api/article.js API / article.js

import { ESTATE_CONFIG } from '../config.js';

export default {
    getarticles: function(){
        return axios.get( ESTATE_CONFIG.API_URL + '/articles' );
    },
}

Store.js Store.js

import Vue from 'vue';
import Vuex from 'vuex';

Vue.use(Vuex);

import { articles } from './modules/articles.js'

export default new Vuex.Store({
    modules: {
        articles,
    }
});

modules/articles.js 模块/ articles.js

import ArticleAPI from '../api/article.js';

export const articles = {
    state: {
        articles: [],
        articlesLoadStatus: 0,

        article: {},
        articleLoadStatus: 0
    },
    getters: {
        getArticlesLoadStatus( state ){
            return state.articlesLoadStatus;
        },
        getArticles( state ){
            return state.articles;
        },
    },
    mutations: {
        setArticlesLoadStatus( state, status ){
            state.articlesLoadStatus = status;
        },
        setArticles( state, articles ){
            state.articles = articles;
        },
    },
    actions: {
        loadArticles( { commit } ){
            commit( 'setArticlesLoadStatus', 1 );

            ArticleAPI.getArticles()
                .then( function( response ){
                  commit( 'setArticles', response.data );
                  commit( 'setArticlesLoadStatus', 2 );
                })
                .catch( function(){
                  commit( 'setArticles', [] );
                  commit( 'setArticlesLoadStatus', 3 );
                });
        },
    },
}

I need help about this. 我需要这方面的帮助。 Because I am not sure what I am doing wrong here. 因为我不确定我在做什么错。 And of course there is no problem with the endpoint. 当然,端点也没有问题。 I can see the json data. 我可以看到json数据。 But my vuex store is empty. 但是我的vuex商店是空的。 And I have an error above. 我上面有一个错误。

The error indicates that an exported function called getArticles does not exist in api/article.js . 该错误表明api / article.js中不存在名为getArticles的导出函数。

Taking a look at that module, it looks like a capitalization issue. 看一下该模块,它看起来像一个大写问题。 The function is not capitalized so when calling it, use: 该函数不大写,因此在调用时,请使用:

ArticleAPI.getarticles

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM