Vetur does not takes account of Vue instances' $variables
See original GitHub issueInfo
- Platform: Win
- Vetur version: 0.22.2
- VS Code version: 1.38.0
Problem
When I declare a global variable in Vue prototype in order to access it from my components, Typescript and TSLint works well but Vetur displays a problem: Property '$http' does not exist on type 'HelloWorld' Vetur(2339)
Reproducible Case
Here is the files involved and the dir. structure (I’m kinda new on Typescript so I don’t know if I did some bad practice
│ App.vue
│ axios.ts
│ main.ts
│ registerServiceWorker.ts
│ router.ts
│ shims-axios.d.ts
│ shims-tsx.d.ts
│ shims-vue.d.ts
│ store.ts
│
├───assets
│ logo.png
│
├───components
│ HelloWorld.vue
│
├───store
│ │ index.ts
│ │
│ └───profile
└───views
About.vue
Home.vue
The error lays in HelloWorld.vue component :
<template>
...
</template>
<script lang="ts">
import Vue from 'vue'
import { Component, Prop } from 'vue-property-decorator'
@Component
export default class HelloWorld extends Vue {
@Prop() private msg!: string
public test() {
return this.$http.post('....') // <== Property '$http' doest not exist on type 'HelloWorld'. Vetur(2339) [89, 17]
}
}
</script>
<style scoped lang="scss">
...
</style>
However everything works fine in the TS compiler. Here’s the rest of the relevant files :
// axios.ts
import axios, { AxiosInstance, AxiosRequestConfig, AxiosResponse } from 'axios'
const config: AxiosRequestConfig = {
baseURL: process.env.API_URL,
timeout: 5000,
}
const http: AxiosInstance = axios.create(config)
// Interceptors declaration....
export default http
// main.ts
import Vue from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
import http from './axios'
import './registerServiceWorker'
Vue.config.productionTip = false
Vue.prototype.$http = http
new Vue({
router,
store,
render: h => h(App),
}).$mount('#app')
// shims-vue.d.ts
declare module '*.vue' {
import Vue from 'vue'
export default Vue
}
// shims-axios.d.ts
import { AxiosInstance } from 'axios'
declare module 'vue/types/vue' {
interface Vue {
$http: AxiosInstance
}
}
And my VSCode workspace settings:
{
"folders": [],
"extensions": {
"recommendations": [
"mikestead.dotenv",
"esbenp.prettier-vscode",
"ms-vscode.vscode-typescript-tslint-plugin",
"octref.vetur"
]
},
"settings": {
"editor.formatOnSave": true,
"vetur.validation.template": true,
"vetur.format.defaultFormatter.scss": "prettier",
"vetur.format.defaultFormatter.css": "prettier",
"vetur.format.defaultFormatter.js": "prettier",
"vetur.format.defaultFormatter.ts": "prettier",
"vetur.experimental.templateInterpolationService": true,
"javascript.validate.enable": false,
"javascript.format.enable": false,
"typescript.format.enable": true,
"tslint.packageManager": "yarn"
}
}
Issue Analytics
- State:
- Created 4 years ago
- Reactions:3
- Comments:5 (2 by maintainers)
Top Results From Across the Web
VSCode showing "cannot find module" TS error for .vue import ...
Seems Vetur looks in the VS Code workspace top-level folder for the tsconfig.json . My Vue app, with its tsconfig.json , is in...
Read more >FAQ | Vetur - GitHub Pages
If you are getting a lot of Property 'xxx' does not exist on type 'CombinedVueInstance' errors, it's an issue with Vue's typing and...
Read more >Vue IntelliSense in VSCode - ITNEXT
I find stand-alone “Vue components” that have a single purpose that you can install from NPM and use in any project are great!...
Read more >Vue.js Debugging: A Guide to Fixing Your Frontend - Snipcart
Learn the basics of Vue.js debugging. This guide will walk you through a tutorial on how to fix your application's frontend.
Read more >15 Typescript Mistakes To Avoid - SoftwareMill Tech Blog
Argument of type 'String' is not assignable to parameter of type 'string'. 'string' is a primitive, but 'String' is a wrapper object. Prefer ......
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
You probably need to import Vue’s typing in
shims-axios.d.ts
before writing augmentation. See: https://vuejs.org/v2/guide/typescript.html#Augmenting-Types-for-Use-with-PluginsFix does not work why trying to use definitions of a local dependency.