首页 > 解决方案 > 使用 TypeScript 的 Vue JS 材料设计

问题描述

我正在尝试将 Vue JS 材料设计与 TypeScript 一起使用,但不支持开箱即用。在GitHub 问题之后,我一直在尝试添加声明自定义模块,但没有成功。我的应用程序是使用vue create my-app命令创建的

我在 src: 中创建了一个新文件types/vue-material/index.d.ts

declare module 'vue-material' {
    import _Vue from "vue";
    
    export function MdButton(Vue: typeof _Vue, options?: any): void
    export function MdContent(Vue: typeof _Vue, options?: any): void
    export function MdTabs(Vue: typeof _Vue, options?: any): void
}

我添加了类型根并从以下位置排除了文件夹tsconfig

{
  "compilerOptions": {
    // ...
    "typeRoots": [
      "src/types",
      "node_modules/@types"
    ]
  },
  // ...
  "exclude": [
    "node_modules",
    "src/types"
  ]
}

但是尝试导入时仍然出现错误VueMaterial

import { MdButton } from 'vue-material';
Could not find a declaration file for module 'vue-material'. 'C:/.../my-app/node_modules/vue-material/dist/vue-material.js' implicitly has an 'any' type.
  Try `npm install @types/vue-material` if it exists or add a new declaration (.d.ts) file containing `declare module 'vue-material';`

我做错了什么?

标签: typescriptvue.jsvue-material

解决方案


推荐阅读