繁体   English   中英

错误 TS2345:“事件”类型的参数不可分配给“字符串”类型的参数

[英]error TS2345: Argument of type 'Event' is not assignable to parameter of type 'string'

嗨,我是 angular 的新手,我似乎无法弄清楚我做错了什么。 这是类型脚本代码。

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

    @Component({
      selector: 'app-root',
      templateUrl: './app.component.html',
      styleUrls: ['./app.component.css']
    })
    export class AppComponent {
      loadedFeature = 'recipe';
    
      onNavigate(feature: string) {
        this.loadedFeature = feature;
      }
    }

下面是 html

<app-header (featureSelected)="onNavigate($event)"></app-header>
<div class="container">
  <div class="row">
    <div class="col-md-12">
      <app-recipies *ngIf="loadedFeature === 'recipe'"></app-recipies>
      <app-shopping-list *ngIf="loadedFeature !== 'recipe'"></app-shopping-list>
    </div>
  </div>
</div>

下面是错误

Error: src/app/app.component.html:1:43 - error TS2345: Argument of type 'Event' is not assignable to parameter of type 'string'.

1 <app-header (featureSelected)="onNavigate($event)"></app-header>
                                            ~~~~~~

  src/app/app.component.ts:5:16
    5   templateUrl: './app.component.html',
                     ~~~~~~~~~~~~~~~~~~~~~~
    Error occurs in the template of component AppComponent.
onNavigate(feature: string) {
    this.loadedFeature = feature;
}

您将feature定义为string类型,显然不是这种情况。 检查 app-header 的文档并添加正确的类型。

更改为 any 现在应该可以解决您的问题,但强烈建议正确输入。

onSelect(feature: string){
        this.featureSelected.emit(feature);
    }

检查 header.component.html 中的代码可能是

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM