(初学者)角度绑定问题

问题描述 投票:0回答:1

我刚刚开始编码。 我发现自己陷入了可能非常简单的事情。 我正在做书上的作业 它几乎没有告诉我该怎么做,但它不起作用。 (screenshot of the book). 用荷兰语写的,所以我翻译:

” 将

<app-camping-registratie (campingRegistratieCreate)="onCampingRegistratieCreated($event)"></app-camping-registratie>
放在页面 app.component.html 中“

在添加上述代码之前,它的工作原理如下: screenshot localhost:4200

添加后我收到以下错误: image errors

index.js:493 [webpack-dev-server] 错误 app/app.component.html:5:82 - 错误 TS2345:“Event”类型的参数不可分配给“string”类型的参数。 5 ~~~~~~ 应用程序/应用程序组件.ts:5:16 5 templateUrl: './app.component.html', ~~~~~~~~~~~~~~~~~~~~~~ 组件AppComponent模板出错

参考这些代码:

import { Component } from '@angular/core';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  campings: string[] = ['De Karnemelkplaats', 'Tro Do Way', 'Camping Kohnenhof', 'Camping Langewald', 'Camping La Fouly'];
registratie: string[] = ['Jaap']

  onCampingCreated(name: string) {
    this.campings.push(name);
  }
  onCampingRegistratieCreated(name: string){
    this.registratie.push(name);
  }
  }

[IMG| app.component.ts](https://i.stack.imgur.com/0Ud2F.png)

<app-camping-invoer (campingCreate)="onCampingCreated($event)"> 
</app-camping-invoer>
<app-camping [campingList]="campings"></app-camping>
<br><br>
<app-camping [campingRegistratieList]="registratie"></app-camping>  
<app-camping-registratie(campingRegistratieCreate)="onCampingRegistratieCreated($event)"></app-camping-registratie>  

[IMG| app.component.html](https://i.stack.imgur.com/dBuAY.png)

链接到 stackblitz 获取整个脚本

也许我真的很愚蠢,因为不理解所给出的错误, 但我真的无法弄清楚这里的问题。 如果有人可以提供建议并解释这里发生的事情,以便我可以从中学习,我将不胜感激。

我多次重新完成整个作业。甚至继续,忽略错误。 according to the book these are supposed to be all the codes

-浆果

angular binding syntax-error
1个回答
0
投票

因此,它确实在错误消息和代码中指出,您的函数 onCampingRegistratieCreated 需要一个字符串作为参数。 $event 不是一个字符串,而是一个对象,它确实需要额外的求解/提取数据。

一种选择是将函数重写为如下所示:

onCampingRegistratieCreated(event: Event) {
   ... // event.? <-- whatever info your would like to push
}

另一种解决方法是设置要推送到模板中该函数的实际字符串,如下所示:

<app-camping-registratie(campingRegistratieCreate)="onCampingRegistratieCreated('Your String')"></app-camping-registratie> 

希望这有帮助。祝你好运!

© www.soinside.com 2019 - 2024. All rights reserved.