简体   繁体   English

依赖周期检测到导入/无周期

[英]dependency cycle detected import/no-cycle

I am trying to set up API endpoints in ES6. 我正在尝试在ES6中设置API端点。 In my main server file, I tried to import the router module but I get the error "dependency cycle detected import/no-cycle". 在我的主服务器文件中,我尝试导入路由器模块,但收到错误“检测到依赖周期的导入/无周期”。 Please find my code below for clearance and assistance. 请在下面找到我的代码以获取许可和帮助。

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;

This could be a direct reference (A -> B -> A) issue, which even you might be doing. 这可能是直接参考(A -> B -> A)问题,甚至您也可能正在这样做。

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

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

Read HERE more about "Eliminate Circular Dependencies from Your JavaScript Project": 这里阅读有关“从JavaScript项目中消除循环依赖”的更多信息:

Once I had the issue in vue.js project and the code that had issue was something like this: 一旦我在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>

Then I fixed it this way: 然后我以这种方式修复它:

<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>

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

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