dva使用yarn编译出错

2022-02-16  本文已影响0人  storyWrite

1. 报错信息

./src/models/example.jsModule build failed: TypeError: /Users/user/Desktop/learn-code/10.React/01_dva-came/src/models/example.js: path.isPrivate is not a function at Array.forEach (<anonymous>)
只要启动项目,然后修改example代码,只要example发生改变就会报该错误,无论是添加注释还是产生新的换行

2.原始代码

// example.js

import { routerRedux } from "dva/router";
// routerRedux == connected-react-redux
function delay(ms) {
  return new Promise((resolve) => {
    setTimeout(() => {
      resolve();
    }, ms);
  });
}

export default {
  namespace: "counter",
  state: {
    count: 1,
  },
  //ddd

  subscriptions: {
    setup({ dispatch, history }) {
      // eslint-disable-line
    },
  },

  effects: {
    *asyncAdd(action, { put, call }) {
      yield call(delay, 1000);

      yield put({ type: "counter/add" });
    },
    *goto({ payload }, { put }) {
      yield put(routerRedux.push(payload));
    },
  },

  reducers: {
    add(state, action) {
      console.log("state", state);

      return {
        count: state.count + 1,
      };
    },
  },
};

// index.js

import dva, { connect } from "dva";
import { Router, Route } from "dva/router";

import React from "react";
import "./index.css";
console.log("Router", Route);

// 1. Initialize
const app = dva();

// 2. Plugins
// app.use({});

// 3. Model
app.model(require("./models/example").default);

// 4. Router
// app.router(require("./router").default);
let Counter = connect((state) => state.counter)((props) => {
  let { count, dispatch } = props;
  console.log("count", props);

  return (
    <div>
      <p>{count}</p>
      <button
        onClick={() =>
          dispatch({
            type: "counter/add",
          })
        }
      >
        +
      </button>
      <button onClick={() => dispatch({ type: "counter/asyncAdd" })}>
        async +
      </button>
      <button onClick={() => dispatch({ type: "counter/goto", payload: "/" })}>
        goto /
      </button>
    </div>
  );
});
const Home = (props) => <div>home</div>;
app.router(({ history }) => (
  <Router history={history}>
    {/* Router 下面只能有一个元素  */}
    <React.Fragment>
      <Route path="/" exact component={Home} />
      <Route path="/counter" component={Counter} />
    </React.Fragment>
  </Router>
));

// 5. Start
app.start("#root");

3.最终查找原因

++++++++++++++++++++++++++++++++++++++++++++++
经过测试发现,使用dva脚手架生成的项目如果后期使用了yarn安装依赖,就会导致出现该错误

上一篇 下一篇

猜你喜欢

热点阅读