Importing Vue components in TypeScript file

前端 未结 4 560
伪装坚强ぢ
伪装坚强ぢ 2020-12-16 15:50

I have been trying to use Vue.js with TypeScript and I came across this repo.

I faced issues here that I am getting error while importing Vue Single Component File f

相关标签:
4条回答
  • 2020-12-16 15:53

    FYI, you can generate . d.ts file for your components by turn on declaration generate in tsconfig. json, copy them to the source dir, see what you've got!

    0 讨论(0)
  • 2020-12-16 15:55

    From Alex Jover:

    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.ts file to your project with the following content:

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

    and write :

    import App from './App.vue'
    

    instead of

    import App from './App'
    
    0 讨论(0)
  • 2020-12-16 15:58

    When using the vue-cli, adapt vue-loader-conf.js as follows:

    var utils = require('./utils')
    var config = require('../config')
    var isProduction = process.env.NODE_ENV === 'production'
    
    module.exports = {
      loaders: utils.cssLoaders({
    
        sourceMap: isProduction
          ? config.build.productionSourceMap
          : config.dev.cssSourceMap,
        extract: isProduction, esModule: true
      }), esModule: true
    }
    
    0 讨论(0)
  • 2020-12-16 16:01

    Check out vue-class-component. Basically, you must add appendTsSuffixTo: [/\.vue$/] to ts-loader's options and esModule: true to vue-loader's options.

    // webpack.config.js
    
    { modules: { rules: [
      {
        test: /\.ts$/,
        exclude: /node_modules|vue\/src/,
        loader: "ts-loader",
        options: { appendTsSuffixTo: [/\.vue$/] }
      },
      {
        test: /\.vue$/,
        loader: 'vue-loader',
        options: {
          esModule: true
        }
      }
    ]}}
    

    I may have missed something else.

    0 讨论(0)
提交回复
热议问题