Reputation: 3234
I'm trying to use typescript with vuejs, normally I use something like
@Component({
props: ['uploadUrl'],
})
export default class SelectionModal extends Vue {
let x = this.uploadUrl // This variable `uploadUrl` is NOT resolved in my IDE
}
but the uploadUrl is not resolved in my IDE (phpStorm)
But we can fix it with vue-property-decorator
, by declaring @Prop like documented here. The code becomes:
<template>
// ...
</template>
<script type="ts">
import {Component, Vue, Prop} from 'vue-property-decorator';
@Component
export default class SelectionModal extends Vue {
@Prop(String) readonly uploadUrl!: string
let x = this.uploadUrl // This variable `uploadUrl` is RESOLVED in my IDE
}
</script>
However, this basic declaration fires such issue
SyntaxError: /myProject/src/path/to/MyComponent.vue: Unexpected token (33:25)
31 |
32 | export default class SelectionModal extends Vue {
> 33 | @Prop(String) readonly uploadUrl!: string
| ^
Anyone here has an idea :
1- how to use the 1st solution and make the uploadUrl
resolved
2- OR how to get ride of the Unexpected token
issue ?
Upvotes: 1
Views: 1469
Reputation: 93728
Your first example doesn't compile - you can't declare let
directly in the class body, and accessing the props through this.prop
causes " Property 'prop' does not exist on type..." error when building the app.
The second code snippet works fine for me when using the right <script>
tag attributes - try changing type
to lang
to have your code treated as a Typescript source:
<script lang="ts">
import {Component, Vue, Prop} from 'vue-property-decorator';
@Component
export default class SelectionModal extends Vue {
@Prop(String) readonly uploadUrl!: string
myMethod(){
let x = this.uploadUrl
}
}
</script>
Upvotes: 2