【问题标题】:Angular2: inline template:8:0 caused by: No provider for StringAngular2:内联模板:8:0 原因:没有字符串提供者
【发布时间】:2017-07-30 23:39:25
【问题描述】:

我在Angular2中写了如下代码:

add-item.component.ts

@Component({
  selector: 'add-item',
  template: `<form-item [title]="abcde"></form-item>`,
})

export class AddItemComponent {
  forms: Object[]

  constructor() {
    this.forms = [{title: 'a', link: 'b'}];
  }
}

form-item.component.ts

@Component({
  selector: 'form-item',
  template: `<h3>{{title}}</h3>`,
  inputs: ['title'],
  styles: [`
    h3 { 
        color: blue;      
    }
  `]
})

export class FormItemComponent{

  @Input()
  title: string;

  constructor(title: string) {
    this.title = title;
  }
}

当我运行这段代码时,我得到一个错误:

错误:./AddItemComponent 类 AddItemComponent 中的错误 - 内联 模板:8:0 原因:没有 String 的提供者!

我做了一个小研究,但我不明白问题出在哪里。我也看过这里: Angular2 EXCEPTION No provider for String

你能帮帮我吗?

【问题讨论】:

标签: angular


【解决方案1】:

您需要从构造函数中删除title: string 和初始化。 Angular 的构造函数期望提供者作为参数。

【讨论】:

    【解决方案2】:

    FormItemComponent 类应该是:

    export class FormItemComponent{
      @Input() title: string = "";
    }
    

    'title' 不能在构造函数中初始化。

    【讨论】:

      猜你喜欢
      • 2017-06-12
      • 2016-05-10
      • 2018-06-14
      • 2016-12-18
      • 2016-04-06
      • 2017-01-31
      • 2016-07-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多