Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Import vue module from another vue module in typescript

Could you please help importing a TypeScript2 Vuejs2 component from another TypeScript2 Vuejs2 component?

Target module (MyTable.vue)

<script lang="ts">
export default {}
</script>

Source module (App.vue)

<template>
  <div>
    <input v-model="msg">
    <p>prop: {{propMessage}}</p>
    <p>msg: {{msg}}</p>
    <p>helloMsg: {{helloMsg}}</p>
    <p>computed msg: {{computedMsg}}</p>
    <button @click="greet">Greet</button>
     <div class="container">
      <my-vuetable></my-vuetable>
    </div>
  </div>
</template>

<script lang="ts">
import Vue from 'vue'
import Component from 'vue-class-component'
import MyVuetable from './MyTable.vue'

@Component({
  props: {
    propMessage: String
  }
})
export default class App extends Vue {
  propMessage: string

  // inital data
  msg: number = 123

  // use prop values for initial data
  helloMsg: string = 'Hello, ' + this.propMessage

  // lifecycle hook
  mounted () {
    this.greet()
  }

  // computed
  get computedMsg () {
    return 'computed ' + this.msg
  }

  // method
  greet () {
    alert('greeting: ' + this.msg)
  }
}
</script>

I am using the latest TS/Webpack/vue-loader/vue-class-component versions.

Notice that similar JS (not TS) based code is working @ https://github.com/ratiw/vuetable-2-tutorial-bootstrap

All the code is @ https://github.com/borislitvak/vue-from-vue-question

Webpack result

App.vue.d.ts  211 bytes          [emitted]
Entrypoint main = build.js build.js.map
   [0] ./~/vue/dist/vue.runtime.esm.js 175 kB {0} [depth 1] [built]
       [exports: default]
       cjs require vue [3] ./~/ts-loader!./~/vue-loader/lib/selector.js?type=script&index=0!./App.vue 19:12-26
       cjs require vue [4] ./~/vue-class-component/dist/vue-class-component.common.js 12:26-40
       cjs require vue [8] ./example.ts 3:12-26
   [1] ./~/process/browser.js 5.3 kB {0} [depth 2] [built]
       cjs require process [0] ./~/vue/dist/vue.runtime.esm.js 1:0-37
       cjs require process [4] ./~/vue-class-component/dist/vue-class-component.common.js 1:0-37
   [2] ./App.vue 1.38 kB {0} [depth 1] [built]
       cjs require ./App.vue [8] ./example.ts 4:16-36
   [3] ./~/ts-loader!./~/vue-loader/lib/selector.js?type=script&index=0!./App.vue 2.09 kB {0} [depth 2] [built]
       cjs require !!ts-loader!./node_modules/vue-loader/lib/selector?type=script&index=0!./App.vue [2] ./App.vue 3:2-93
   [4] ./~/vue-class-component/dist/vue-class-component.common.js 4.02 kB {0} [depth 3] [built]
       cjs require vue-class-component [3] ./~/ts-loader!./~/vue-loader/lib/selector.js?type=script&index=0!./App.vue 20:28-58
   [5] ./~/vue-loader/lib/component-normalizer.js 1.12 kB {0} [depth 2] [built]
       cjs require !./node_modules/vue-loader/lib/component-normalizer [2] ./App.vue 1:16-78
   [6] ./~/vue-loader/lib/template-compiler.js?id=data-v-52143112!./~/vue-loader/lib/selector.js?type=template&index=0!./App.vue 1.12 kB {0} [depth 2] [built]
       cjs require !!./node_modules/vue-loader/lib/template-compiler?id=data-v-52143112!./node_modules/vue-loader/lib/selector?type=template&index=0!./App.vue [2] ./App.vue 5:2-152
   [7] (webpack)/buildin/global.js 509 bytes {0} [depth 2] [built]
       cjs require global [0] ./~/vue/dist/vue.runtime.esm.js 1:0-44
   [8] ./example.ts 300 bytes {0} [depth 0] [built]

ERROR in ....\App.vue.ts
(20,24): error TS2307: Cannot find module './MyTable.vue'.

I am new to client side development, please help!

Thank you,

Boris

like image 874
Boris Avatar asked Mar 22 '17 07:03

Boris


People also ask

How to use typescript with Vue single file components?

How To Use TypeScript with Vue Single File Components 1 Setting Up the Project. With Vue CLI 3+, it is possible to generate a new project with TypeScript already configured. ... 2 Configuring the TypeScript Compiler. You can configure TypeScript to the needs of your team or project. ... 3 Using Basic and Custom Data Types. ... More items...

How to use a custom data type in a Vue component?

You can now use this custom data type in any Single-File Vue Component ( .vue) or TypeScript ( .ts) file. Here is an example for App.vue that uses the User interface and displays the firstName and lastName: In order to use TypeScript in this component, you will need to add a lang attribute to the script tag of your component.

How do I import an app from another Vue module?

If your editor is yelling at the line import App from './App' in main.js file about not finding the App module, you can add a vue-shim.d.tsfile to your project with the following content: declare module "*.vue" { import Vue from 'vue' export default Vue } and write : import App from './App.vue'

Can I import a Vue file into a TS file?

Thus when you import any .vuefile into a .tsfile, it is considered of type Vue. Not sure what Vue does with the declaration, but I guess you can declare the module in any .d.tsfile in root directory since TS will process all .d.tsfiles automatically – HMD Jan 28 '21 at 9:23 1


3 Answers

The answer was given by https://github.com/ktsn and the original can be found @ https://github.com/vuejs/vue/issues/5298:

This is because you don't have declarations of .vue files, then the typescript compiler cannot load them. You need to declare general declaration of .vue file in your project or generating each .vue file declaration by using vuetype

I've double checked that the above works, followed the general declaration path. Note that most vue components don't come with t.ds definitions, thus you'll have to write them yourselves for TS to be able to compile the files.

Enjoy! Boris

like image 173
Boris Avatar answered Dec 15 '22 02:12

Boris


use this

module: {
    rules: [
        {
            test: /\.ts?$/,
            loader: 'ts-loader',
            options: {
                appendTsSuffixTo: [/\.vue$/]
            }
        },
        {
            test: /\.vue$/,
            loader: 'vue-loader'
        }
    ]
like image 25
heithem moumni Avatar answered Dec 15 '22 03:12

heithem moumni


If you put the following code into a 'custom.d.ts' file at the same folder structure to where your vue files are, it should work. Boris put it beautifully and basically webpack does not recognise .vue file definitions.

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

The problem has been explained in my blog post here: https://danpottsdoes.wordpress.com/2017/09/28/unit-testing-vue-class-components-using-typescript-chai-sinon-my-findings

like image 32
DanPottsHimself Avatar answered Dec 15 '22 02:12

DanPottsHimself