【发布时间】:2020-12-09 17:10:34
【问题描述】:
我目前正在尝试在 Angular 9 中创建一个动态菜单。我对 Angular 很陌生,并且由于某种原因收到以下错误消息: “错误无法解析 C:/mypath/head-menu.component.ts 中 HeadMenuComponent 的所有参数:(?, ?, ?)。”代码很简单:
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 {
}
}
代码正在编译,ng serve 可以工作,但我仍然收到错误消息。这对我来说尤其重要,因为 angular-cli command:ng xi18n 不会因为这个错误而运行。
所以我的问题是:“我做错了什么?”还有另一种方法可以将对象传递给构造函数吗?使用角注入感觉不对,只是为了给构造函数传一个字符串,但是我还没有找到其他方法。
【问题讨论】:
标签: angular angular-cli angular-components constructor-injection