小编Geo*_*tan的帖子

无法在 Angular 9 中进行默认导入

我通过添加此属性更改了tsconfig.json

"esModuleInterop": true, "allowSyntheticDefaultImports": true,

为了能够导入 npm 包 import * as ms from "ms";

但我仍然收到这个错误

This module is declared with using 'export =', and can only be used with a default import when using the 'allowSyntheticDefaultImports' flag.
Run Code Online (Sandbox Code Playgroud)

我错过了什么?

更新:

如果我更改为import ms from "ms",那么它可以与编译器一起正常工作,但不能与 VSCode linter 一起使用,并且错误是

 can only be default-imported using the 'allowSyntheticDefaultImports' flagts(1259)
index.d.ts(25, 1): This module is declared with using 'export =', and can only be used with a default import when using the 'allowSyntheticDefaultImports' …
Run Code Online (Sandbox Code Playgroud)

javascript node.js typescript angular

11
推荐指数
4
解决办法
2万
查看次数

Electron Forge - 无法在渲染器文件中使用 ipcRenderer

我刚刚使用以下命令创建了一个新应用程序:

npx create-electron-app my-new-app --template=typescript-webpack
Run Code Online (Sandbox Code Playgroud)

renderer.ts 中,我添加了以下代码

import "./index.css";
import { ipcRenderer } from "electron";
Run Code Online (Sandbox Code Playgroud)

但是当我运行npm run start 时,浏览器控制台中出现以下错误

Uncaught ReferenceError: require is not defined
Run Code Online (Sandbox Code Playgroud)

更新 我尝试过的内容:

webpack.plugins.js

const ForkTsCheckerWebpackPlugin = require("fork-ts-checker-webpack-plugin");
const webpack = require("webpack");
module.exports = [
  new ForkTsCheckerWebpackPlugin(),
  new webpack.ExternalsPlugin("commonjs", ["electron"]),
];
Run Code Online (Sandbox Code Playgroud)

但它仍然不起作用。

node.js electron electron-forge

9
推荐指数
2
解决办法
2274
查看次数

将 NestJS 连接到 websocket 服务器

NestJS 如何用作 websocket 客户端?我想使用 NestJS 作为客户端连接到远程 websocket 服务器,但我在框架中没有找到有关此实现的任何信息。

websocket stream-socket-client nestjs

6
推荐指数
2
解决办法
5429
查看次数

独立组件中的模拟独立指令

我正在尝试为独立组件编写单元测试并模拟其依赖性。该独立组件具有以下内容:

import { Component, OnInit } from '@angular/core';
import { CommonModule } from '@angular/common';
import { DemoDirectiveDirective } from './demo-directive.directive';

@Component({
  selector: 'app-demo-cmp',
  standalone: true,
  imports: [CommonModule,DemoDirectiveDirective],
  templateUrl: './demo-cmp.component.html',
})
export class DemoCmpComponent implements OnInit {
  constructor() {}

  ngOnInit(): void {}
}
Run Code Online (Sandbox Code Playgroud)

并且 DemoDirectiveDirective 具有以下内容:

import { Directive, ElementRef } from '@angular/core';

@Directive({
  selector: '[appDemoDirective]',
  standalone: true,
})
export class DemoDirectiveDirective {
  constructor(private hostElement: ElementRef) {
    this.hostElement.nativeElement.innerHTML += 'Update from directive! ';
  }
}
Run Code Online (Sandbox Code Playgroud)

DemoCmpComponent我想这样测试:

import { ComponentFixture, TestBed …
Run Code Online (Sandbox Code Playgroud)

jestjs angular angular-standalone-components

5
推荐指数
2
解决办法
2358
查看次数

NestJS - 在异常过滤器中返回 ACK

我有以下示例:

@SubscribeMessage('v1-test')
@UsePipes(ValidationPipe)
@UseFilters(ValidationOnSocketExceptionFilter)
async test(
  @MessageBody() payload: payloadDTO,
  @ConnectedSocket() client: Socket,
){
   .....do stuff......
   return true;
}
Run Code Online (Sandbox Code Playgroud)

这是 ValidationOnSocketExceptionFilter:

 @Catch(BadRequestException)
export class ValidationOnSocketExceptionFilter
  implements ExceptionFilter<BadRequestException> {
  private readonly logger = new Logger(ValidationOnSocketExceptionFilter.name);

  constructor(private readonly customResponseService: CustomResponseService) {}

  catch(exception: BadRequestException, host: ArgumentsHost) {
    console.log('Validation err on socket');

    const client = host.switchToWs().getClient();

    // * get the msg from all validation erros
    let constraints: string[] | string;

    if (typeof exception.getResponse() === 'object') {
      let err: any = exception.getResponse();
      if (err.message && Array.isArray(err.message)) …
Run Code Online (Sandbox Code Playgroud)

websocket node.js nestjs

4
推荐指数
1
解决办法
1325
查看次数

类验证器 @ValidateIf() 无法正常工作

我在 Nest js 中有以下类和此类验证器:

 @ValidateIf(val => val !== '*')
 @IsObject()
 @IsNotEmptyObject()
 queryParams: DbQuery | '*';
Run Code Online (Sandbox Code Playgroud)

如果我发送 '*' 它会返回

 [ 'queryParams must be a non-empty object' ] 
Run Code Online (Sandbox Code Playgroud)

node.js class-validator nestjs

2
推荐指数
1
解决办法
2万
查看次数

NestJS 阻止服务器在开发中重新加载

我有一条路线,在public目录中创建一个新文件夹,该文件夹是由app.useStaticAssets提供的静态内容。

问题是,即使我在tsconfig.build.jsontsconfig.json排除数组中添加了公共目录,当在公共目录中删除或创建新文件夹时,我的服务器仍然会在开发中重新加载。

我错过了什么?

更新: tsconfig.json

{
 "compilerOptions": {
    "module": "commonjs",
    "declaration": true,
    "removeComments": true,
    "noImplicitAny": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "esModuleInterop": true,
    "target": "es2017",
    "sourceMap": true,
    "outDir": "./dist",
    "baseUrl": "./",
    "incremental": true
  },
  "exclude": ["node_modules", "dist", "public"]
}
Run Code Online (Sandbox Code Playgroud)

我提到public文件夹位于src文件夹之外。他们处于同一水平。

node.js nestjs

2
推荐指数
1
解决办法
1723
查看次数

Nest JS微服务TCP端到端测试

有人知道如何为 Nest 微服务编写 E2E 测试吗?给这个代码?

主要.ts

import { NestFactory } from '@nestjs/core';
import { Transport } from '@nestjs/microservices';
import { AppModule } from './app.module';

async function bootstrap() {
  const app = await NestFactory.createMicroservice(AppModule, {
    transport: Transport.TCP,
  });
  app.listen(() => console.log('Microservice is listening'));
}
bootstrap();
Run Code Online (Sandbox Code Playgroud)

应用程序控制器.ts

import { Controller } from '@nestjs/common';
import { MessagePattern } from '@nestjs/microservices';

@Controller()
export class MathController {
  @MessagePattern({ cmd: 'sum' })
  accumulate(data: number[]): number {
    return (data || []).reduce((a, b) => a + b);
  } …
Run Code Online (Sandbox Code Playgroud)

microservices e2e-testing nestjs

1
推荐指数
1
解决办法
3105
查看次数

RXJS 在管道运算符内将事件转换为 KeyboardEvent

我正在尝试添加 keydown 事件并将参数类型从 Event 转换为 KeyboardEvent,但收到以下 TS 错误。

fromEvent(document, "keydown")
  .pipe<KeyboardEvent, KeyboardEvent>(
    filter((event) => event.code === "F5"),
    tap((event) => event.preventDefault())
  )
  .subscribe((ev) => {
    console.log(ev);
  });
Run Code Online (Sandbox Code Playgroud)

但我收到以下错误:

error TS2345: Argument of type 'MonoTypeOperatorFunction<Event>' is not assignable to parameter of type 'OperatorFunction<Event, KeyboardEvent>'.
  Type 'Observable<Event>' is not assignable to type 'Observable<KeyboardEvent>'.
    Type 'Event' is missing the following properties from type 'KeyboardEvent': altKey, charCode, 
code, ctrlKey, and 17 more.
Run Code Online (Sandbox Code Playgroud)

有人遇到过这个问题吗?

rxjs typescript angular

1
推荐指数
1
解决办法
417
查看次数