我是Vue的新手,想学习如何创建组件并将包发布到NPM。因此,我的想法是创建Vue(typescript)+Vuetify可重用组件,并从NPM在任何其他项目中安装它们。我成功地在vue.js项目中导入了我的HelloWorld组件,但是当我尝试在Vue ts项目中导入它时,我会遇到以下错误:
'Could not find a declaration file for module 'sastify'. '/home/raphael/tester-ts/node_modules/sastify-nv/dist/sastify.common.js' implicitly has an 'any' type.
Try npm install @types/sastify-nv
if it exists or add a new declaration (.d.ts) file containing declare module 'sastify-nv';
如何生成类似于我的JS项目的.d.ts文件?
我的项目树是:
── babel.config.js
├── package.json
├── postcss.config.js
├── README.md
├── src
│ ├── components
│ │ └── HelloWorld
│ │ └── HelloWorld.vue
│ └── sastify.js
├── tsconfig.json
├── tslint.json
└── yarn.lock
package.json:
{
"name": "sastify-nv",
"version": "0.1.6",
"private": false,
"scripts": {
"serve": "vue-cli-service serve",
"build": "vue-cli-service build --target lib --name sastify ./src/sastify.js",
"lint": "vue-cli-service lint --fix",
"build:ts": "tsc"
},
"files": [
"dist",
"src"
],
"main": "dist/sastify.common.js",
"dependencies": {
"vue": "^2.6.10",
"vue-property-decorator": "^8.1.0",
"vuetify": "^2.0.0"
},
"devDependencies": {
"@vue/cli-plugin-typescript": "^3.11.0",
"@vue/cli-service": "^3.11.0",
"sass": "^1.17.4",
"sass-loader": "^7.1.0",
"typescript": "^3.4.3",
"vue-cli-plugin-vuetify": "^0.6.3",
"vue-template-compiler": "^2.6.10",
"vuetify-loader": "^1.2.2"
}
}
tsconfig.json:
{
"compilerOptions": {
"target": "esnext",
"module": "esnext",
"strict": true,
"jsx": "preserve",
"importHelpers": true,
"declaration": true,
"outDir": "lib",
"moduleResolution": "node",
"experimentalDecorators": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"sourceMap": true,
"baseUrl": ".",
"types": [
"webpack-env",
"vuetify"
],
"paths": {
"@/*": [
"src/*"
]
},
"lib": [
"esnext",
"dom",
"dom.iterable",
"scripthost"
]
},
"include": [
"src/**/*.ts",
"src/**/*.tsx",
"src/**/*.vue",
"tests/**/*.ts",
"tests/**/*.tsx"
],
"exclude": [
"node_modules"
]
}
sastify.js:
满足需求的JavaScript库。export { default as HelloWorld } from './components/HelloWorld/HelloWorld.vue'
组件/HelloWorld/HelloWorld.vue:
<template>
<v-card>Vcard from HelloWorld</v-card>
</template>
<script lang="ts">
import { Component, Vue } from "vue-property-decorator";
import { VCard } from "vuetify/lib";
@Component({
name: "HelloWorld",
components: {
VCard
}
})
export default class HelloWorld extends Vue {}
</script>
<style scoped>
</style>