在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 問(wèn)答/HTML5/ Property 'getInstance' does not exist on

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("北京", "北京", "朝陽(yáng)區(qū)", "xx 街道 xx 號(hào)");
          } else {
            return new Address("西藏", "拉薩", "xx區(qū)", "xx 街道 xx 號(hào)");
          }
        }
      },
      {
        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 {
    //設(shè)置
    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'.

回答
編輯回答
陌離殤

clipboard.png

2017年7月5日 18:04
編輯回答
厭惡我

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'.
是因?yàn)?-----》

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

下面這兩個(gè)報(bào)錯(cuò)是因?yàn)?--》 id寫(xiě)成了Id,賦值錯(cuò)了!!

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

2017年7月29日 03:50
編輯回答
伐木累

getInstance是Id的實(shí)例屬性,不是靜態(tài)屬性,ID.getInstance當(dāng)然不正確了, ts提示的錯(cuò)誤大概就這個(gè)意思。

關(guān)于其他的地方我看到不正確的地方包括:

  • FactoryProvider中使用的注入的實(shí)例對(duì)象請(qǐng)用deps聲明,具體可以看文檔
  • 使用@Inject裝飾器聲明的屬性必須是ng相關(guān)的(component等)或者像service一樣,加一個(gè)@Injectable,這個(gè)最新版本好像可以不用加了,但是當(dāng)前版本還是需要的。

另外你可能對(duì)ts本身的使用方式不太熟悉? 你這里的ID、Address、Person等對(duì)象使用interface來(lái)約束更好一些

2018年1月8日 20:11