小编yuv*_*.le的帖子

NestJS:如何在@Query 对象中转换数组

我是 NestJS 的新手,我正在尝试从查询参数中填充过滤器 DTO。

这是我所拥有的:

询问:

本地主机:3000/api/checklists?stations=114630,114666,114667,114668

控制器

@Get()
public async getChecklists(@Query(ValidationPipe) filter: ChecklistFilter): Promise<ChecklistDto[]> {
    // ...
}
Run Code Online (Sandbox Code Playgroud)

DTO

export class ChecklistFilter {

    @IsOptional()
    @IsArray()
    @IsString({ each: true })
    @Type(() => String)
    @Transform((value: string) => value.split(','))
    stations?: string[];

    // ...
}
Run Code Online (Sandbox Code Playgroud)

有了这个,类验证器不会抱怨,然而,在过滤器对象中,站实际上不是一个数组,而是一个单一的字符串。

我想将其转换为验证管道中的数组。我怎样才能做到这一点?

class-validator nestjs class-transformer

7
推荐指数
4
解决办法
9042
查看次数

ngx-bootstrap 手风琴动态打开面板

我使用 ngx-bootstrap 手风琴来显示博客文章列表。

这是模板:

<accordion id="blog-list">
    <accordion-group *ngFor="let post of posts; let first = first;" [isOpen]="first" id="post-{{post.id}}">
        <!-- Here goes content irrelevant to the question -->
    </accordion-group>
</accordion>
Run Code Online (Sandbox Code Playgroud)

我还使用一些全局配置,一次只打开一个折叠面板。

export function getAccordionConfig(): AccordionConfig {
  return Object.assign(new AccordionConfig(), { closeOthers: true });
}
Run Code Online (Sandbox Code Playgroud)

现在,当帖子更新时,我会在列表中更新它,如下所示:

constructor(private elementRef: ElementRef, private postService: PostService) {
    this.postService.updatedPost.subscribe(val => {
      let i = this.posts.findIndex(post => post.id === val.id);
      this.posts[i] = val;
      let element = elementRef.nativeElement.querySelector('#post-' + val.id);
      element.setAttribute('isOpen', true); // <- this does not work
      element.scrollIntoView(true);
    }); …
Run Code Online (Sandbox Code Playgroud)

javascript bootstrap-accordion ngx-bootstrap angular

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