I encountered a strange problem while developing an angular app. I wrote this piece of code some time ago and it's working perfectly:
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
}
now, selectedGeoArea
is then passed as input to another component
<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
}
the problem is that buildQuery()
always returns the value of addressQuery
, meaning that the switch is not working at all. selectedGeoArea
has the correct value and type as set in receiveStoreEvent()
.
what am I missing here?
You either need to do the following
this.selectedGeoArea = event.constructor
or the following
switch (this.selectedGeoArea.constructor) {
.
.
.
}
In the initial function you were matching for event.constructor
but in the new function you were matching only for event
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.