1

I have a Vue project that uses single file class component and typescript loader. I use VS Code with the Vetur plugin to get very nice code completion inside single file .vue components. This works fine except in my entry.ts file, where I create the root Vue component:

In entry.ts, I cannot import vue modules!

index.ts

import Vue from 'vue'                       // this is ok
import App from './components/app.vue'      // vue file not found!
new App({el : '#app'})

ERROR Cannot find module './components/app.vue

app.vue

<script lang="ts">
    import Vue from 'vue'                       // this is ok
    import Card from "./card.vue"               // here it works!

    export default class App extends Vue {
    }
</script>

From a .vue file, I can import other .vue modules.

I have tried leaving out the .vue extension, but that has the same result.

I have tried adding the .vue extension to a .d.ts file, but that messes up the Vetur plugin, and reverses the problem (modules only work from .ts files but not from .vue files)

1 Answer 1

6

Hi Typescript gives you that error because It doesn't know what .vue file is.

But there's a work around for it.

In your project directory you can add a file called vue-shims.d.ts

Inside put the following code:

declare module "*.vue" {
  import Vue from "vue";
  export default Vue;
}

Now typescript will understand .vue files.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, but that .d.ts file causes glitches with the Vetur plugin (which is needed to work with .vue files). intellisense becomes very glitchy and module paths are not recognised anymore in Vetur after I add the type definition file.
Even with this it doesn't work, it can't even import non-ts .vue files in. And it's selective, some files work, some don't.
If you uses Vue3, you have to import: import type {DefineComponent} from 'vue' and export const component: DefineComponent<object, object, any>; export default component

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.