javascript 在 VSCode 中导出函数时:“个别声明必须全部导出或全部为本地”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31295608/
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
In VSCode when exporting functions: "Individual declarations must be all exported or all local"
提问by clearwater
I recently upgraded to Visual Studio Code 0.5.0 and some new errors cropped up that weren't there before.
我最近升级到 Visual Studio Code 0.5.0 并且出现了一些以前没有的新错误。
I have a bunch of functions that are declared locally and then exported. Since the upgrade, however, hovering over each of the local function names produces the error Individual declarations in merged declaration functionNamemust be all exported or all local.
我有一堆在本地声明然后导出的函数。然而,由于升级,将鼠标悬停在每个本地函数名称上会产生错误合并声明functionName 中的单独声明必须全部导出或全部为本地。
This is an example local function that is exported.
这是导出的示例本地函数。
var testParamsCreatorUpdater = function (lTestParams, creatorID){
lTestParams.creator = creatorID;
return lTestParams;
};
module.exports.testParamsCreatorUpdater = testParamsCreatorUpdater;
I realize I can change this to...
我意识到我可以将其更改为...
module.exports.testParamsCreatorUpdater = function (lTestParams, creatorID){
lTestParams.creator = creatorID;
return lTestParams;
};
And prepend module.exports.to every testParamsCreatorUpdater()call.
并在module.exports前面加上。到每个testParamsCreatorUpdater()调用。
But why is the first snippet wrong? As I understand it, require() makes everything in the module.exports object available to whatever required it.
但是为什么第一个片段是错误的?据我了解,require() 使 module.exports 对象中的所有内容都可用于任何需要它的地方。
回答by leoncc
I think at a JavaScript level it cannot differentiate between:
我认为在 JavaScript 级别它无法区分:
var testParamsCreatorUpdater = ...
and
和
module.exports.testParamsCreatorUpdater = ...
as the names are the same. I got the exact same error (leading me to this post) in TypeScript when I tried this:
因为名字是一样的。当我尝试这个时,我在 TypeScript 中遇到了完全相同的错误(引导我到这篇文章):
import { AuditService } from '../services/audit.service';
import { Audit } from '../models/audit.model';
@Component({
selector: 'audit',
templateUrl: './audit.component.html',
})
export class Audit {
constructor(private auditService: AuditService) {
}
}
So TypeScript did not like that I imported a module called Auditand exported a class also called Audit.
所以 TypeScript 不喜欢我导入了一个名为Audit的模块并导出了一个也称为Audit的类。
回答by redcomethk
I think it's related to the feature of merged declaration for TypeScript ref. I have not done the detailed research for Typescript but it seems that it can include Javascript in the Typescript file.
我认为这与 TypeScript ref的合并声明功能有关。我还没有对 Typescript 进行详细研究,但它似乎可以在 Typescript 文件中包含 Javascript。
I guess the way testParamsCreatorUpdater was declared in the Javascript was detected to be error by VSCode because it thinks the two declarations cannot be merged.
我猜在 Javascript 中声明 testParamsCreatorUpdater 的方式被 VSCode 检测到是错误的,因为它认为这两个声明不能合并。