javascript 依赖循环检测到导入/无循环
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/51094117/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
dependency cycle detected import/no-cycle
提问by Darotudeen
I am trying to set up API endpoints in ES6. 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.
我正在尝试在 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;
回答by Syed
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 HEREmore about "Eliminate Circular Dependencies from Your JavaScript Project":
在此处阅读有关“从您的 JavaScript 项目中消除循环依赖”的更多信息:
Once I had the issue in vue.jsproject 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>

