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'.

阅读 4.9k
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'.
是因为------》

export class Id {
  static getInstance(type: string): Id {
    //设置
    return new Id();
  }
}

下面这两个报错是因为---》 id写成了Id,赋值错了!!

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 (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提示的错误大概就这个意思。

关于其他的地方我看到不正确的地方包括:

  • FactoryProvider中使用的注入的实例对象请用deps声明,具体可以看文档
  • 使用@Inject装饰器声明的属性必须是ng相关的(component等)或者像service一样,加一个@Injectable,这个最新版本好像可以不用加了,但是当前版本还是需要的。

另外你可能对ts本身的使用方式不太熟悉? 你这里的ID、Address、Person等对象使用interface来约束更好一些