Property 'getInstance' does not exist on type 'typeof Id'.
原文为:
import { Component, Inject, ReflectiveInjector } from "@angular/core";
import { OverlayContainer } from "@angular/material";
import { environment } from "../environments/environment";
@Component({
selector: "app-root",
templateUrl: "./app.component.html",
styleUrls: ["./app.component.css"]
})
export class AppComponent {
darkTheme = false;
squareState: string;
constructor(private oc: OverlayContainer) {
const injector = ReflectiveInjector.resolveAndCreate([
{ provide: Person, useClass: Person },
{
provide: Address,
useFactory: () => {
if (this.darkTheme) {
return new Address("北京", "北京", "朝阳区", "xx 街道 xx 号");
} else {
return new Address("西藏", "拉萨", "xx区", "xx 街道 xx 号");
}
}
},
{
provide: Id,
useFactory: () => {
return Id.getInstance("idcard");
}
}
]);
const person = injector.get(Person);
console.log(JSON.stringify(person));
}
switchTheme(dark) {
this.darkTheme = dark;
this.oc.themeClass = dark ? "myapp-dark-theme" : null;
}
}
export class Id {
getInstance(type: string): Id {
//设置
return new Id();
}
}
export class Address {
province: string;
city: string;
district: string;
street: string;
constructor(province, city, district, street) {
this.province = province;
this.city = city;
this.district = district;
this.street = street;
}
}
export class Person {
id: Id;
address: Address;
constructor(@Inject(Id) id, @Inject(Address) address) {
this.id = Id;
this.address = Address;
}
}
ERROR in E:/前端/ng2/999/now/0405newNg/my-app/src/app/app.component.ts (30,21): Property 'getInstance' does not exist on type 'typeof Id'.
ERROR in E:/前端/ng2/999/now/0405newNg/my-app/src/app/app.component.ts (69,5): Type 'typeof Id' is not assignable to type 'Id'.
Property 'getInstance' is missing in type 'typeof Id'.
ERROR in E:/前端/ng2/999/now/0405newNg/my-app/src/app/app.component.ts (70,5): Type 'typeof Address' is not assignable to type 'Address'.
Property 'province' is missing in type 'typeof Address'.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
ERROR in E:/前端/ng2/999/now/0405newNg/my-app/src/app/app.component.ts (30,21): Property 'getInstance' does not exist on type 'typeof Id'.
是因为------》
下面这两个报错是因为---》 id写成了Id,赋值错了!!
ERROR in E:/前端/ng2/999/now/0405newNg/my-app/src/app/app.component.ts (69,5): Type 'typeof Id' is not assignable to type 'Id'.
Property 'getInstance' is missing in type 'typeof Id'.
ERROR in E:/前端/ng2/999/now/0405newNg/my-app/src/app/app.component.ts (70,5): Type 'typeof Address' is not assignable to type 'Address'.
Property 'province' is missing in type 'typeof Address'.
getInstance是Id的实例属性,不是静态属性,
ID.getInstance
当然不正确了, ts提示的错误大概就这个意思。关于其他的地方我看到不正确的地方包括:
另外你可能对ts本身的使用方式不太熟悉? 你这里的ID、Address、Person等对象使用interface来约束更好一些