无法在类中应用`interface`

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

我创建了一个用于分配“语言”的模型,如下所示:

export interface ModelLang {
    lang:string;
}

与app状态一样添加了相同的内容:

import { ModelLang } from "./shared-components/state/models";

export interface AppState {
    lang:ModelLang
}

但是当我在app组件中使用时,将字符串输入错误作为Eng not assignable to type ModelLang - 这里的类型出错了什么?

app.component.ts:

/ ** *保持近乎理想。只是为了保持路由器加载目的。 * *为app导入jquery !! * /

import { Component, OnInit } from '@angular/core';
import { Store, select } from "@ngrx/store";
import { AppState } from "./app.state";
import { Lang } from "./shared-components/state/shared.actions";
import { ModelLang } from "./shared-components/state/models";
import * as $ from 'jquery';

@Component({
    selector: 'app-root',
    templateUrl: './app.component.html',
    styleUrls: ['./app.component.scss']
})
export class AppComponent implements OnInit {

    lang:ModelLang = "Eng"; //throws error

    constructor(private store:Store<AppState>){}

    ngOnInit(){
    }


}

Live Demo

angular7 typescript2.0
1个回答
1
投票

您正在尝试将字符串分配给类型变量,请尝试以下操作。

long:ModelLang = {lang:“Eng”};

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