在我的 .netcore 应用程序中,我将我的 Typescript 版本从 2.4.1
升级到 3.2.4
,现在我收到错误 Cannot find name 'setImmediate '
在编译时。我下面的实现基本上直接来自 Microsoft guide关于预渲染。
export default createServerRenderer(params => {
const providers = [
{ provide: INITIAL_CONFIG, useValue: { document: '<app></app>', url: params.url } },
{ provide: APP_BASE_HREF, useValue: params.baseUrl },
{ provide: 'BASE_URL', useValue: params.origin + params.baseUrl },
];
return platformDynamicServer(providers).bootstrapModule(AppModule).then(moduleRef => {
const appRef: ApplicationRef = moduleRef.injector.get(ApplicationRef);
const state = moduleRef.injector.get(PlatformState);
const zone = moduleRef.injector.get(NgZone);
return new Promise<RenderResult>((resolve, reject) => {
zone.onError.subscribe((errorInfo: any) => reject(errorInfo));
appRef.isStable.pipe(first(isStable => isStable)).subscribe(() => {
setImmediate(() => { // <!-- error here
resolve({
html: state.renderToString(),
globals: {
}
});
moduleRef.destroy();
});
});
});
});
});
我看到那里was a bug raised for this ,但是它被标记为已解决和合并。我还尝试安装 setImmediate
包并导入它,但这也不起作用。任何人都知道如何解决这个问题?
最佳答案
非 webpack:
安装 npm install --save-dev @types/node
然后在文件开头导入 import '@types/node';
解决了这个问题我。
网络包:
如果您使用的是 webpack,那么这将不起作用。相反,npm install --save-dev @types/node node
那么你将不得不:
进入您的 .tsconfig
并添加 'node'
:
{
"compilerOptions": {
"types": [..., "node" ],
...
}
}
不要 import '@types/node';
这是出于 here 所述的原因:
@types
packages are stored in the@types
directory but they must never be manually imported. Rather, you need to import the dependency they correspond to from where that dependency is installed.@types
packages provide the type declarations for dependencies that do not otherwise provide them.
关于node.js - 语法错误 : Cannot find name 'setImmediate' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54805607/