角度中的类或函数实用程序



如何以及何时在 angular2 中创建实用程序/辅助程序类或函数?

我已经为将要创建的所有常规函数和类创建了一个名为 helpers 的文件夹。

但现在我有这个代码,它将用于 8 个组件。

/**
   * this is initialize after the elements have been loaded. called inside ngAfterViewInit methods
   * this sets up the joyrideService
   */
  joyRideInit(): void {
    const fileNameNotes = this.designNotes.map((matchImgUrl) =>
      extractKeyFromFilename(matchImgUrl)
    );
    const seenNotes = {
      viewed_notes: {
        match: fileNameNotes,
        ...this.viewedNotes
      }
    };
    this.joyrideService
      .startTour({
        steps: [
          'matchFirst',
          ...this.designNotes,
          'guidedTourBtn',
          'saveNextBtn'
        ]
      })
      .subscribe(null, null, () => {
        this.updateUserNotes(seenNotes);
      });
  }
  /**
   * sends a resquest to update the notes
   * @param userUpdateNotes 
   */
  updateUserNotes(userUpdateNotes): void {
    this.userService
      .updateUser(userUpdateNotes, this.user.id)
      .pipe(
        first(),
        map((result) => {
          // * result no used.
          console.log('res', result);
        }),
        catchError((error) => {
          console.error('display-notes#nextNoteOrClose: error in ', error);
          throw error;
        })
      )
      .subscribe();
  }

我应该把它作为一个类还是函数? 这将有 4 个参数来自将使用它的组件。

创建共享/公共服务,所有像这样的方法,默认情况下你可以把它放在那里进行注入。

最新更新