クラスのすべてのパラメーターを解決できません(?)

Aug 20 2020

現在、Angular 9で動的メニューを作成しようとしています。Angularは初めてで、何らかの理由で次のエラーメッセージが表示されます。「C:/ mypath / head-のHeadMenuComponentのすべてのパラメーターを解決できません。 menu.component.ts:(?、?、?)。」コードは非常に単純です。

import { Component, OnInit, Inject, Injectable } from '@angular/core';

  @Component({
    selector: 'app-head-menu',
    templateUrl: './head-menu.component.html',
    styleUrls: ['./head-menu.component.css']
  })

  export class HeadMenuComponent implements OnInit {
    imageURL: string;
    text: string;
    menuFunction: () => void;

  constructor(@Inject(String)imageURL: string, @Inject(String)text: string, 
              @Inject(Function)functionToAccept: () => void) {
    this.imageURL = imageURL;
    this.text = text;
    this.menuFunction = functionToAccept;
   }


  ngOnInit(): void {
  }

}

コードはコンパイル中であり、ngserveは機能しますが、それでもエラーメッセージが表示されます。このエラーのためにangular-cliコマンド:ng xi18nが実行されないため、これは私にとって特に重要です。

だから私の質問は:「私は何が間違っているのですか?」オブジェクトをコンストラクターに渡す別の方法はありますか?文字列をコンストラクターに渡すためだけに角度インジェクションを使用するのは私には間違っていると感じますが、別の方法をまだ見つけていません。

回答

1 Ric Aug 20 2020 at 18:19

これを行う1つの方法は、次のことを行うことです。

export const IMAGE_URL = new InjectionToken<string>('imageUrl');

次に、app.module(または他の場所で):

{
  provide: IMAGE_URL ,
  useValue: 'www.someurl.com'
}

または

{
  provide: IMAGE_URL,
  deps: [ConfigService],   //dependencies
  useFactory: (c: ConfigService) => {
      return c.someUrlValue();
  }
}

そして消費する:

constructor(@Inject(IMAGE_URL) imageUrl: string) {

}