vue-property-decorator icon indicating copy to clipboard operation
vue-property-decorator copied to clipboard

Vue router issue when using reflect metadata

Open moltar opened this issue 6 years ago • 7 comments
trafficstars

When I enable reflect metadata as per doc, I get these errors:

"export 'RawLocation' was not found in 'vue-router'

This actually happens when setting emitDecoratorMetadata.

My use case is pretty simple:

import { RawLocation } from 'vue-router'

I just want to type annotate some code that it requires RawLocation-compatible data.

moltar avatar Apr 24 '19 02:04 moltar

@moltar

Hi.

Actually, this issue is caused by enabling emitDecoratorMetadata . I recommend you to set the option to false.

kaorun343 avatar Apr 24 '19 20:04 kaorun343

Hm, but the README says that it needs to be enabled.

https://github.com/kaorun343/vue-property-decorator#if-youd-like-to-set-type-property-of-each-prop-value-from-its-type-definition-you-can-use-reflect-metadata

Thanks!

moltar avatar Apr 25 '19 00:04 moltar

Hi, why was this closed? Is this not a valid issue?

moltar avatar Apr 25 '19 10:04 moltar

It is optional to set emitDecoratorMetadata to true.

@Component
class MyComponent extends Vue {
  @Prop() age: number
}

When the component is wrote like this, the component is going to be converted to like this if the options is set to false.

Vue.extend({
  name: 'MyComponent',
  props: {
    age: { type: null }
  }
})

Whereas, if it is set to true and reflect-metadata is imported, the component is going to be converted to like this.

Vue.extend({
  name: 'MyComponent',
  props: {
    // `type` is automatically set based on its type definition in TypeScript world
    age: { type: Number } 
  }
})

Reference https://github.com/kaorun343/vue-property-decorator/issues/107

kaorun343 avatar Apr 25 '19 13:04 kaorun343

As well as the issue I introduced, it's seemed that you are using @Watch property.

@Component
class MyComponent extends Vue {
  @Watch('rawLocation')
  onChange(newLocation: RawLocation) {}
}

When emitDecoratorMetadata is enabled, TypeScript compiler try to refer RawLocation object to pass the type information ( for example, newLocation is a RawLocation). The type definition of RawLocation is defined, but it is not exported as an actual JS value.

kaorun343 avatar Apr 25 '19 13:04 kaorun343

Whereas, if it is set to true and reflect-metadata is imported, the component is going to be converted to like this.

Right, and that is exactly what I wanted! :)

As well as the issue I introduced, it's seemed that you are using @Watch property.

I am not using Watch in the same context as RawLocation.


Thank you!

moltar avatar Apr 26 '19 05:04 moltar

Same issue here, with @Prop.

import { RawLocation } from 'vue-router'
// …
@Prop() to?: RawLocation    // warning: export 'RawLocation' was not found in 'vue-router'

I've worked around it by setting the type explicitly:

import { RawLocation } from 'vue-router'
// …
@Prop({ type: [Object, String] }) to?: RawLocation

It would be nice if vue-property-decorator would simply treat missing exports as Object. The following behavior was unexpected:

import { Location } from 'vue-router'
// …
@Prop() to?: string | Location    // warning: export 'Location' was not found in 'vue-router'

In this case the library infers type as Object but there's clearly a string in there, so it should be [Object, String].

fluidsonic avatar Nov 05 '19 06:11 fluidsonic