接口抛出错误无法理解

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

这是我的代码:

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

export interface Lang {
  name:string;
}

@Component({
  selector: 'my-app',
  styleUrls: ['./app.component.scss'],
  templateUrl: './app.component.html',
})

export class AppComponent {

  lang:Lang = "Eng";//getting error as not assignable..!?
  readonly name: string = 'Angular';
  version: number = 7;

  onVersionRelease(): void {
    this.version += 1;
  }
}

Live Link

为什么Eng不能分配给接口字符串lang?任何帮助我理解。以及如何为字符串属性创建接口呢?

typescript typescript-typings
1个回答
1
投票

您的界面要求您的对象必须像这样声明:

  lang: Lang = {name: 'Eng'};
© www.soinside.com 2019 - 2024. All rights reserved.