最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

为什么vscode不能自己自动完成来自nodejs中另一个文件中的另一个文件的导出对象?它是一项性能功能吗?

网站源码admin21浏览0评论

为什么vscode不能自己自动完成来自nodejs中另一个文件中的另一个文件的导出对象?它是一项性能功能吗?

为什么vscode不能自己自动完成来自nodejs中另一个文件中的另一个文件的导出对象?它是一项性能功能吗?

问题

在我的用例中,我在单独的文件sequelize中使用models/index.js维护我的数据库配置/连接。我export数据库连接对象(称为db),以便其他模块可以连接到数据库。但是,当我在其他文件中require该对象时,则不会显示该对象的自动完成及其相应的属性。

上下文

这是我的models/index.js文件的一部分的样子,就像我的官方sequelize example一样:-

const { Sequelize } = require('sequelize');
const sequelize = new Sequelize(config);
let db = {};
// for each of the files `file` in a dir...dynamically do(refer above linked example for details):-
let model = require(path.join(__dirname, file))(sequelize, Sequelize);
db[model.name] = model; // type Sequelize.Model
// dynamic config over

// statically add
db.sequelize = sequelize; // type Sequelize
module.exports = db

这是我在另一个模块中的导入方式:-

const db = require("./models/index.js");
db.seq -------------------------------> where i try for auto-completion

期望

[当我require我的数据库连接对象db在另一个文件中时,它应该为我静态分配的属性提供自动完成功能,例如db.sequelize应该是一个完成,然后是db.sequelize.xxx,其中xxx是相应的sequelize方法和属性也应自动完成。

现在的行为是什么?

键入db.不会显示与对象db有关的任何有效自动完成。另外db.sequelizeany部分的类型为sequelize(因此不再进行自动填充),而我希望它是Sequelize

我做了什么?

  • 明确添加jsdoc,以为这是使自动完成有效的唯一方法,但无济于事:-
/**
* @typedef {import('sequelize').Sequelize} Sequelize
*/

const { Sequelize } = require('sequelize');
/**
 * @type {Sequelize}
 */
const sequelize = new Sequelize(config);
/**
 * key : string -> Name of the Sequelize.Model | 'sequelize'
 * value : Either one of the Models or Sequelize object
 * @type {Object.<string,(Sequelize|Sequelize.Model)>}
 * @property {Sequelize} sequelize
 * @example
 * db = { User: db.User , seqeuelize : sequelize}
 */
let db = {};
// rest of the dynamic processing and adding Sequelize.Model to db
db.sequelize = sequelize;
module.exports = db
  • 添加了具有以下内容的jsconfig.json,但这也没有用:-
{
  // NOTE THAT, this file resides within my src/server/ directory where all the js files exists
  // the models/index.js resides within src/server
  // I maintain another jsconfig for src/client
  "compilerOptions": {
    "module": "commonjs",
    "target": "es6",
    "allowSyntheticDefaultImports": true,
    "moduleResolution": "node",
    "experimentalDecorators":true,
    "baseUrl": ".",
    "paths": {
      "*": ["*"],
    },
  },
  "include": ["./**/*"],
}
回答如下:

如果将对象let db = {};更改为let db = {sequelize: null};,则会获得智能,可以按预期工作。

发布评论

评论列表(0)

  1. 暂无评论