Property 'getInstance' does not exist on type 'typeof Id'.

发布于 2022-09-07 03:25:22 字数 2229 浏览 15 评论 0

原文为:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

热风软妹 2022-09-14 03:25:22

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

谜兔 2022-09-14 03:25:22

getInstance是Id的实例属性,不是静态属性,ID.getInstance当然不正确了, ts提示的错误大概就这个意思。

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

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

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

缪败 2022-09-14 03:25:22

clipboard.png

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文