【问题标题】:dependency cycle detected import/no-cycle检测到依赖循环导入/无循环
【发布时间】:2018-06-29 04:07:11
【问题描述】:

我正在尝试在 ES6 中设置 API 端点。在我的主服务器文件中,我尝试导入路由器模块,但出现错误“检测到依赖周期导入/无周期”。请在下面找到我的代码以获得许可和帮助。

import express from 'express';

import bodyParser from 'body-parser';

import router from './routes/routes';

const app = express();
const PORT = process.env.PORT || 8080;

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
// app.use(routes);

app.use('/api/v1', router);

const run = () => console.log('way to go server!');

app.listen(PORT, run);
export default app;

【问题讨论】:

  • 该问题不包含可能负责循环依赖、./routes/routes 和其他任何代码的代码。问题应包含stackoverflow.com/help/mcve

标签: javascript node.js express ecmascript-6


【解决方案1】:

这可能是直接参考(A -> B -> A) 问题,甚至您也可能正在这样做。

// file a.ts
import { b } from 'b';
...
export a;

// file b.ts
import { a } from 'a';
...
export b;

阅读 HERE 更多关于“消除 JavaScript 项目中的循环依赖”的信息:

一旦我在 vue.js 项目中遇到问题并且出现问题的代码是这样的:

<script>
  import router from '@/router';
  import { requestSignOut } from '../../api/api';

  export default {
    name: 'sign-out',
    mounted() {
      requestSignOut().then((data) => {
        if (data.status === 'ok') {
          router.push({ name: 'sign-in' });
        }
      });
    },
  };
</script>

然后我这样修复它:

<script>
import { requestSignOut } from '@/api/api';

export default {
  name: 'sign-out',
  mounted() {
    requestSignOut().then((data) => {
      if (data.status === 'ok') {
        this.$router.push({ name: 'sign-in' });
      }
    });
  },
};
</script>

【讨论】:

    猜你喜欢
    • 2013-08-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-07
    • 2020-11-08
    • 2019-01-10
    相关资源
    最近更新 更多