【问题标题】:Argument of type '{}' is not assignable to parameter of type 'string'“{}”类型的参数不能分配给“字符串”类型的参数
【发布时间】:2018-06-24 22:54:21
【问题描述】:

这是我的打字稿

export class State {
  constructor(public name: string, public population: string, public flag: string) { }
}



export class AutocompleteOverviewComponent implements OnInit {
      stateCtrl: FormControl;
      filteredStates: Observable<any[]>;
    temp:string;
      states: State[] = [
        {
          name: 'Arkansas',
          population: '2.978M',
          // https://commons.wikimedia.org/wiki/File:Flag_of_Arkansas.svg
          flag: 'https://upload.wikimedia.org/wikipedia/commons/9/9d/Flag_of_Arkansas.svg'
        },
        {
          name: 'California',
          population: '39.14M',
          // https://commons.wikimedia.org/wiki/File:Flag_of_California.svg
          flag: 'https://upload.wikimedia.org/wikipedia/commons/0/01/Flag_of_California.svg'
        },
        {
          name: 'Florida',
          population: '20.27M',
          // https://commons.wikimedia.org/wiki/File:Flag_of_Florida.svg
          flag: 'https://upload.wikimedia.org/wikipedia/commons/f/f7/Flag_of_Florida.svg'
        },
        {
          name: 'Texas',
          population: '27.47M',
          // https://commons.wikimedia.org/wiki/File:Flag_of_Texas.svg
          flag: 'https://upload.wikimedia.org/wikipedia/commons/f/f7/Flag_of_Texas.svg'
        }
      ];

      constructor() {
        this.stateCtrl = new FormControl();

      }
   ngOnInit() {
        this.filteredStates = this.stateCtrl.valueChanges
        .pipe(
          startWith(''),
          map(state => state ? this.filterStates(state) : this.states.slice(),
        )
        );
      }

      filterStates(name1:string) {
        return this.states.filter(state =>
          state.name.toLowerCase().indexOf(name1.toLowerCase()) === 0);
      }

我收到错误,例如 ** '{}' 类型的参数不可分配给 'string' 类型的参数。** 在 ngOnInit 函数上我正在调用 this.filterStates(state) 函数,但我收到错误请帮助他

【问题讨论】:

  • 删除对startWith的调用。
  • 删除 startwith 后仍然无法正常工作。
  • 嗯,这取决于你想要完成什么,但你的startWith 将导致Observable&lt;string | State&gt;
  • 这是一个角材料自动完成的例子,所以我想知道我哪里出错了。

标签: angular typescript angular-material


【解决方案1】:

您可以将“val”变量的类型从“string”更改为“any”,它应该可以工作。

filter(val: any): string[] { return this.options.filter(option => option.toLowerCase().includes(val.toLowerCase())); }

【讨论】:

    【解决方案2】:

    我不是 100% 确定,但可能是因为您的 filterStates 函数正在接收一个字符串:

    filterStates(name1:string) {
      ...
    }
    

    但你正在传递一个对象:

    map(state => state ? this.filterStates(state)
    

    【讨论】:

    • state 是 map 的值,所以它不是对象,State 是类,states 是对象。
    • 我在做这个例子:"stackblitz.com/angular/…".
    • 我已将您的代码复制并粘贴到 stackblitz 中,但没有收到任何错误。确保包含所有依赖项和所有导入
    • 出现这样的错误 D:/mobicable/src/app/component/shared/autocomplet.component.ts (55,48) 中的错误:“{}”类型的参数不是可分配给“字符串”类型的参数。 D:/mobicable/src/app/component/shared/autocomplet.component.ts (55,48) 中的错误:“{}”类型的参数不可分配给“字符串”类型的参数。跨度>
    猜你喜欢
    • 1970-01-01
    • 2021-10-21
    • 2021-10-13
    • 1970-01-01
    • 2021-09-04
    • 1970-01-01
    • 1970-01-01
    • 2019-04-18
    相关资源
    最近更新 更多