typescript Angular 事件发射器错误:应为0个类型参数,但得到了1个

ippsafx7  于 2023-05-08  发布在  TypeScript
关注(0)|答案(4)|浏览(140)

我得到了类型错误,“期望0类型参数,但得到1”,尽管遵循本教程到T。https://youtu.be/I317BhehZKM?t=57s
我已指定:

newUserInfoComplete:boolean = false

然而,我在这行代码中得到了上面在<boolean>上指定的错误:

@Output() newUserInfoCompleteEvent = new EventEmitter <boolean> ();

另外,如果我简单地忽略<boolean>,我会得到这个错误:

Argument of type 'boolean' is not assignable to parameter of type 'string'.

这个.NewUserInfoComplete在这里加了下划线:

this.newUserInfoCompleteEvent.emit(this.newUserInfoComplete);

下面是我的组件:

import { Component, OnInit, Output } from '@angular/core';
import { slideToRight } from '../../../../router.animations';
import { Router, ActivatedRoute, UrlSegment } from '@angular/router';
import { EventEmitter } from 'protractor';

@Component({
  selector: 'app-new-user-input',
  templateUrl: './new-user-input.component.html',
  styleUrls: ['./new-user-input.component.css'],
  animations: [slideToRight()]
})
export class NewUserInputComponent implements OnInit {

  newUserInfoComplete:boolean = false

  @Output() newUserInfoCompleteEvent = new EventEmitter <boolean> ();

  constructor(private router: Router, r: ActivatedRoute) {
    r.url.subscribe((s: UrlSegment[]) => {
      console.log("url", s); //https://vsavkin.com/angular-router-understanding-router-state-7b5b95a12eab
    });
  }

  ngOnInit() {
  }


  sendNewUserInfoComplete(){
    this.newUserInfoCompleteEvent.emit(this.newUserInfoComplete);
  }

  displaySibling() {
    console.log(this.router);
    this.router.navigate(['../', { outlets: { newuserorginfo: ['newuserorginfo'] } }])
  }

  closeBlade() {
    this.router.navigate([{ outlets: { newuserinput: null } }]);
  }

}
jmp7cifd

jmp7cifd1#

尝试从@angualar/core而不是从protractor导入EventEmitter

import { Component, OnInit, Output, EventEmitter } from '@angular/core';
wfveoks0

wfveoks02#

我忘了检查我的进口!糟糕,我用的是PROTRACTOR的事件发射器,而不是角核事件发射器

import { EventEmitter } from '@angular/core';
import { Component, OnInit, Output } from '@angular/core';
import { slideToRight } from '../../../../router.animations';
import { Router, ActivatedRoute, UrlSegment } from '@angular/router';

@Component({
  selector: 'app-new-user-input',
  templateUrl: './new-user-input.component.html',
  styleUrls: ['./new-user-input.component.css'],
  animations: [slideToRight()]
})
export class NewUserInputComponent implements OnInit {

  newUserInfoComplete = false;

  @Output() newUserInfoCompleteEvent = new EventEmitter<boolean>();

  constructor(private router: Router, r: ActivatedRoute) {
    r.url.subscribe((s: UrlSegment[]) => {
      console.log("url", s); //https://vsavkin.com/angular-router-understanding-router-state-7b5b95a12eab
    });
  }

  ngOnInit() {
  }


  sendNewUserInfoComplete() {
    this.newUserInfoCompleteEvent.emit(this.newUserInfoComplete);
  }

  displaySibling() {
    console.log(this.router);
    this.router.navigate(['../', { outlets: { newuserorginfo: ['newuserorginfo'] } }])
  }

  closeBlade() {
    this.router.navigate([{ outlets: { newuserinput: null } }]);
  }

}
1mrurvl1

1mrurvl13#

正如上面的答案中提到的,不要从'*protractor '导入EventEmitter,而是从'@angular/core *'导入。
为什么会发生:因为你在导入它之前使用了EventEmmiter,所以在这种情况下它会自动从'protractor'模块导入。

bjp0bcyl

bjp0bcyl4#

import { Component, Output } from '@angular/core';
import { slideToRight } from '../../../../router.animations';
import { Router, ActivatedRoute, UrlSegment } from '@angular/router';
import { EventEmitter } from 'protractor';

@Component({
  selector: 'app-new-user-input',
  templateUrl: './new-user-input.component.html',
  styleUrls: ['./new-user-input.component.css'],
  animations: [slideToRight()]
})
export class NewUserInputComponent {

  newUserInfoComplete = false;
  @Output() newUserInfoCompleteEvent = new EventEmitter <boolean> ();

  constructor(private router: Router, r: ActivatedRoute) {
    r.url.subscribe((s: UrlSegment[]) => {
      console.log("url", s);
    });
  }
  sendNewUserInfoComplete(){
    this.newUserInfoCompleteEvent.emit(!!this.newUserInfoComplete);
  }

  displaySibling() {
    console.log(this.router);
    this.router.navigate(['../', { outlets: { newuserorginfo: ['newuserorginfo'] } }])
  }

  closeBlade() {
    this.router.navigate([{ outlets: { newuserinput: null } }]);
  }

}

试试这个

相关问题