我创建了一个用于分配“语言”的模型,如下所示:
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(){
}
}
您正在尝试将字符串分配给类型变量,请尝试以下操作。
long:ModelLang = {lang:“Eng”};