简体   繁体   English

TypeScript文件中导入Vue组件

[英]Importing Vue components in TypeScript file

I have been trying to use Vue.js with TypeScript and I came across this repo .我一直在尝试将 Vue.js 与 TypeScript 一起使用,我遇到了这个 repo

I faced issues here that I am getting error while importing Vue Single Component File from TypeScript. I am using Visual Studio Code.我在这里遇到了从 TypeScript 导入 Vue 单一组件文件时出错的问题。我正在使用 Visual Studio Code。 Please see error below.请参阅下面的错误。

main.ts:主.ts:

// The Vue build version to load with the 'import' command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import * as Vue from 'vue'
import App from './App'

/* eslint-disable no-new */
new Vue({
    el: '#app',
    template: '<App/>',
    components: { App }
})

VS Code error: VS代码错误:

 1 ERROR • main.ts [ts] Cannot find module './App'. (4 17)

使用 visual studio 代码在 main.ts 中导入 App.vue 的问题

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:如果您的编辑器对 main.js 文件中的 import App from './App' 行大喊大叫关于找不到 App 模块,您可以将vue-shim.d.ts文件添加到您的项目中,其内容如下:

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

and write :和写 :

import App from './App.vue'

instead of代替

import App from './App'

Check out vue-class-component .查看vue-class-component Basically, you must add appendTsSuffixTo: [/\\.vue$/] to ts-loader 's options and esModule: true to vue-loader 's options.基本上,您必须将appendTsSuffixTo: [/\\.vue$/]添加到ts-loader的选项,并将esModule: truevue-loader的选项。

// 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.我可能错过了别的东西。

When using the vue-cli, adapt vue-loader-conf.js as follows:使用vue-cli时,修改vue-loader-conf.js如下:

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
}

FYI, you can generate .仅供参考,您可以生成 . d.ts file for your components by turn on declaration generate in tsconfig.通过在 tsconfig 中打开声明生成来为您的组件创建 d.ts 文件。 json, copy them to the source dir, see what you've got! json,将它们复制到源目录,看看你得到了什么!

Had this issue even with a vue.d.ts because I had multiple declarations in the same file.即使是vue.d.ts也有这个问题,因为我在同一个文件中有多个声明。 Had to split them up like this:不得不像这样拆分它们:

In vue 3 this is the vue.d.ts that has to be somewhere in a path that is included in your tsconfig.json.在 vue 3 中,这是vue.d.ts ,它必须位于 tsconfig.json 中包含的路径中的某处。

// vue.d.ts
declare module '*.vue' {
    import { DefineComponent } from 'vue'
    // eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/ban-types
    const component: DefineComponent<{}, {}, any>
    export default component
}

Also I wanted to set global properties on my vue instance and have them available in any component via intellisense.我还想在我的 vue 实例上设置全局属性,并通过智能感知在任何组件中使用它们。 After adding the following code to my vue.d.ts file, the editor complained when I imported.vue files into typescript. So I had to add a separate vue-runtime.d.ts file , or it wouldn't work:在我的vue.d.ts文件中加入如下代码后,编辑器在我import.vue文件到typescript时报错,所以只好单独加一个vue-runtime.d.ts文件,不然不行:

// vue-runtime.d.ts
import '@vue/runtime-core'

declare module '@vue/runtime-core' {
    interface ComponentCustomProperties {
        customFunction: (val: number) => string;
    }
}

Then customFunction will be registered on all components!然后customFunction将在所有组件上注册!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM