dvajs / dva

🌱 React and redux based, lightweight and elm-style framework. (Inspired by elm and choo)
https://dvajs.com/
MIT License
16.25k stars 3.16k forks source link

dva mock数据api出现404,但是我写了接口 #357

Closed jasminecjc closed 7 years ago

jasminecjc commented 7 years ago

model/funnel.js

import { query } from '../services/funnel';

export default {
    namespace: 'funnel',

    state: {
        funnelList: [],
        dimension: [],
        chart: [],
        details: [],
        loading: false,
        modalVisible: false,
        modalType: 'create'
    },

    subscriptions: {
        setup({ dispatch, history }) {
            history.listen(location => {
                if(location.pathname === '/funnel') {
                    dispatch({
                        type: 'query',
                        payload: {}
                    })
                }
            })
        }
    },

    effects: {
        *query({ payload }, { select, call, put }) {
            yield put({ type: 'showLoading' });
            const { data } = yield call(query);
            if (data) {
                yield put({
                    type: 'querySuccess',
                    payload: {
                        chart: data.chart,
                    }
                });
            }
        },
        *create(){},
        *addTo(){}
    },

    reducers: {
        showLoading(state, action){
            return { ...state, loading: true };
        }, // 控制加载状态的 reducer
        showModal(){}, // 控制 Modal 显示状态的 reducer
        hideModal(){},
        // 使用服务器数据返回
        querySuccess(state, action) {
            return {...state, ...action.payload, loading: false};
        },
        createSuccess(){},
        addToSucces(){},
    }
};

services/funnle.js

import request from '../utils/request';
import qs from 'qs';

export async function query(params) {
    return request(`/api/funnel?${qs.stringify(params)}`);
}

mock/funnel.js

'use strict';

const mockjs = require('mockjs');

module.exports = {

  'GET /api/funnel': function (req, res) {
    const a = [100, 80, 60, 40, 20].reverse();
    const data = mockjs.mock({
        chart: {
            data: ['曝光', '下载', '注册', '平台首次呼叫', '首单'].reverse(),
            seriesData: {
                stayData: a,
                lostData: a.map(item => 100 - item),
            }
        }   
    });
   res.json({
      success: true,
      chart: data.chart 
    });
  },

};

报错:

request.js:25 GET http://localhost:8989/api/funnel? 404 (Not Found)
sorrycc commented 7 years ago

重启 npm start 试试,另外直接访问 http://localhost:8989/api/funnel 也是 404 ?

jasminecjc commented 7 years ago

重启了,直接访问接口 ,控制台的报错是 proxy: proxy.config.js parse error: Error: Cannot find module 'mockjs',不知道为什么我没remove过什么npm modules,安装了mockjs之后解决了