打字稿:打开对象类型无效

时间:2020-03-26 10:55:16

标签: javascript angular typescript switch-statement

在开发角度应用程序时遇到了一个奇怪的问题。 我前段时间写了这段代码,它运行良好:

selectedGeoArea: any

receiveStoreEvent(event) {
    switch (event.constructor) {
      case City:
        console.log("city")
        break
      case Province:
        console.log("province")
        break
      case String:
        console.log("region")
        break
    }
    this.selectedGeoArea = event
  }

现在,selectedGeoArea然后作为输入传递到另一个组件

<text-search  [selectedGeoArea]="selectedGeoArea"></text-search>


export class TextSearchComponent {
  @Input() selectedGeoArea: any

  buildQuery(): string {
    switch (this.selectedGeoArea) {
      case City:
        return `${this.addressQuery}, ${this.selectedGeoArea.name}, ${this.selectedGeoArea.province.code}, ${this.selectedGeoArea.province.region}`
      case Province:
        return `${this.addressQuery}, ${this.selectedGeoArea.code}, ${this.selectedGeoArea.region}`
      case String:
        return `${this.addressQuery}, ${this.selectedGeoArea}`
    }
    return this.addressQuery
  }

问题在于buildQuery()始终返回addressQuery的值,这意味着该开关根本不起作用。 selectedGeoArea具有receiveStoreEvent()中设置的正确值和类型。

我在这里想念什么?

1 个答案:

答案 0 :(得分:1)

您需要执行以下操作

org.seedstack.seed.SeedException: [CORE] Multiple seedstack launchers

Description
-----------
There are multiple SeedStack entry points in the classpath.

Fix
---
Exactly one class implementing 'org.seedstack.seed.spi.SeedLauncher' should be present in the classpath and registered in
META-INF/services. Remove all but one launcher.

或以下

this.selectedGeoArea = event.constructor

在初始功能中,您匹配的是switch (this.selectedGeoArea.constructor) { . . . } ,但在新功能中,您仅匹配了event.constructor