-
-
Notifications
You must be signed in to change notification settings - Fork 33.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Cannot import from a typescript Vue component into another typescript Vue component #5298
Comments
This is because you don't have declarations of |
I cannot thank you enought for your answer, @ktsn ! You've made my day! Without knowing the above, how would you approach debugging this problem? Do you 'node debug webpack'? If not, how do you know where the problem is? After all, you don't see the generated .d.ts file nor there are any webpack/vue-loader logs. Thank you, |
Just see the compiler error log. |
Happy someone else had the same question. Thank you! |
THANK YOU !! |
Oh! Why this isn't added into official doc? |
I can't seem to get this thing working without vuetype. Is using vuetype mandatory? |
I had almost the same problem, but the above only solved it within Visual Studio Code. When running the development server I got the following error: Module build failed: Error: Could not find file: '/src/sections/search-result/search-result.vue'. I solved it by changing the webpack ts-loader config from:
to
like they do in the upcoming typescript template for vue-cli. |
@ktsn Including the index.ts
app.vue
|
Including
|
u can import component without use suffix
<template>
<div class="home">
home ...
</div>
</template>
<script lang="ts" src="./Home.ts">
</script>
import { Vue } from 'vue-property-decorator';
@Component
export default class Home extends Vue {}
// doesn't work
// import Home from './views/Home.vue';
// well done
import Home from './views/Home' |
@ktsn I previously used the general vue declaration ("vue-shim") you linked to, but I'm facing a problem now that I'm trying to augment the Vue type with a custom property on the prototype: In order for VS Code to recognize the plugin, I have to declare a module for 'vue/types/vue':
This conflicts with the vue-shim, which declares a module for '*.vue', so now I have TS support for my |
@TychoCRD I'm also running into this issue, I was wondering if you ended up solving it? My last resort here is to use |
@ktsn Thank you. But it is not resolve. Then i try resolve as TypeScript-Vue-Starter,
it resolve. |
@borislitvak @ktsn @korziee In my case , when I try to import some {
"include": [
"src/**/*.ts",
"src/**/*.vue",
"test/**/*.ts"
],
"exclude": [
"node_modules"
],
"files": [
"ts-custom.d.ts"
],
"types": [
"jest"
],
"compilerOptions": {
"outDir": "./dist/",
"target": "es5",
"module": "esnext",
"moduleResolution": "node",
"esModuleInterop": true,
"strict": true,
"noImplicitReturns": true,
"allowJs": false,
"sourceMap": true,
"pretty": true,
"removeComments": true
}
} and finally, vs code feel so happy. :laugh: |
Hey @ktsn your link "https://github.com/vuejs/vue-class-component/blob/master/example/sfc.d.ts" is broken. I can't find its equivalent through google. I have this shim that I use in my projects:
But this obviously doesn't work for named exports. vuetype doesn't appear to work for them either. I'd like to find out if there's a way to use Vue and TypeScript with named exports because they cut down on potential typos. |
@beetaa Many thanks for saving my time. For those unfamiliar like me, important elements are:
It is implemented here |
The link provided in the upvoted comment is broken. What is the solution to this problem as of Jun, 2019? |
As of today, vuetype has not been updated since February 2018 and won't work with non-class-based Vue component declaration. Seems like we still don't have a way to directly import |
Then the obvious question is: Why is this still closed then? |
@skovmand Your solution worked for me as well, but i haven't notice it untill I disabled the "Vetur" extension in Visual Studio Code. It keeps the red error underline when importing Vue components without |
The |
Ugh forget that. I just forgot to include the
|
I also have to include the .vue extension when importing components or I get the same errror. I am using
It seems there is something missing still. I have tried using shims-vue.d.ts in the files section with no results.. Any help would be appreciated. :) |
@NavidMitchell Does not work:
Does work:
It seems the issue is in ts-loader..
|
I followed the upvoted comment but it didn't work. Looks like there was a breaking change, but the semver ^4.1.0 will not work. |
If |
I gave up on generating proper
I do this:
Definitely easier than faffing around with
It does compile but Typescript thinks that My ultimate solution is to switch to React which is simply much much better at integrating with Typescript (it also type checks templates which is a pretty huge flaw in Vue). |
@Timmmm |
This answer is downvoted, but for some reason only this works. Though I do not undrstand why... |
Hi @Timmmm, the |
If I want to also separate template, is there any workaround? |
Well, you can move the style part into a standalone file and reference it as you do for the script part. In that way you only get the template part in the .vue file |
For Vue3 (found in @FredKSchott 's snowpack + vue + typescript template) Declare the type of your declare module '*.vue' {
import { defineComponent } from 'vue';
const component: ReturnType<typeof defineComponent>;
export default component;
} then tell typescript to include it in {
"include": [
"types",
// ...
],
// ...
} |
@ninofiliu I did that but I still cannot import Vue components via import MyComponent from "@/components/MyComponent"; only via import MyComponent from "@/components/MyComponent.vue"; Is there any other change required? |
@Anubarak well yes, if you write |
@ninofiliu Thank you very much for your answer. That explains it. Guess I'll just always import them as |
What's the reason for the vue shim not to be included in vue's own typescript support? Do I really have to have this 'noise' file in my project that could easily be in the library I'm working with? My webpack build doesn't compile without this thing: shims-vue.d.ts
|
@ninofiliu I have found that the To be more specific, when you are using Vue 3 + options api + using a declare module '*.vue' {
import { DefineComponent } from 'vue';
const component: DefineComponent<{}, {}, any>;
export default component;
} This is also the solution taken in the Vue 3 Codesandbox playground and also suggested by Vetur docs. |
Hey everyone, I tried import a Component class inside a .ts file, a mixin. In .vue's files import occurs normal, no error in VsCode, but import inside a .ts's file the erro show up. Adding a ts-shim file with *.vue declaration, the error gone, but I lose CTRL + (go to) in all .vue imports, even inside .vue's files (that have no errors without ts-shim) :/ Any one have a better solution? |
Version
2.2.6
Reproduction link
https://github.com/borislitvak/vue-from-vue-question
Steps to reproduce
I've asked on stackoverflow and vue forum, no answer for several days. I am new to client side development.
What is expected?
I can import the MyTable.vue into App.vue.
Both components are writting in Typescript.
NOTE: I can import the TS vue component into another TS file, i.e., App.vue into example.ts.
What is actually happening?
During webpack build:
ERROR in ....\App.vue.ts
(20,24): error TS2307: Cannot find module './MyTable.vue'.
https://forum.vuejs.org/t/how-can-i-view-vue-loader-logs-and-generated-files/8350
http://stackoverflow.com/questions/42945415/import-vue-module-from-another-vue-module-in-typescript
The text was updated successfully, but these errors were encountered: