[英]Error: Could not find declaration file for module VueJS application
Hi I am using NuxtJS to build a VueJS application.您好我正在使用 NuxtJS 构建一个 VueJS 应用程序。 I have installed an image cropping library vue-croppie .
我已经安装了一个图像裁剪库vue-croppie 。 I have imported the Vue component as per the documentation like below
我已经按照下面的文档导入了 Vue 组件
import VueCroppie from 'vue-croppie'
However, I am getting the following error on import statement但是,我在导入语句中收到以下错误
Could not find a declaration file for module 'vue-croppie'.
找不到模块“vue-croppie”的声明文件。 'xxx/node_modules/vue-croppie/dist/vue-croppie.cjs.js' implicitly has an 'any' type.
'xxx/node_modules/vue-croppie/dist/vue-croppie.cjs.js' 隐含了一个 'any' 类型。 Try
npm i --save-dev @types/vue-croppie
if it exists or add a new declaration (.d.ts) file containing `declare module 'vue-croppie';如果存在,请尝试
npm i --save-dev @types/vue-croppie
或添加包含 `declare module 'vue-croppie' 的新声明 (.d.ts) 文件;
I have tried declaring index.d.ts
file at the root of my project with following content but id doesn't solve the problem我已经尝试在我的项目的根目录中声明
index.d.ts
文件并包含以下内容,但 id 并不能解决问题
declare module 'vue-croppie';
I have tried using require
like below as suggested on other posts but of no use我已经尝试使用下面的
require
,就像其他帖子中所建议的那样,但没有用
const VueCroppie = require('vue-croppie')
I understand this is a Typescript issue but have no knowledge about Typescript.我知道这是 Typescript 问题,但不了解 Typescript。 Can somebody throw more light on this.
有人可以对此进行更多说明。 What is happening and how to fix it.
发生了什么以及如何解决它。
Thanks谢谢
The issue was I had not installed the plugin with VueJS app.问题是我没有使用 VueJS 应用程序安装插件。 Here is how to do it.
这是如何做到的。
VueJS VueJS
We can use Vue.use to isntall a plugin before it can be used like below我们可以使用Vue.use来安装插件,然后才能使用它,如下所示
import Vue from 'vue';
import VueCroppie from 'vue-croppie';
Vue.use(VueCroppie)
NuxtJS NuxtJS
In case of NuxtJS it is little different.在 NuxtJS 的情况下,它几乎没有什么不同。 The plugin is registered globally, here's how.
该插件在全球范围内注册,方法如下。
Create a file called vue-croppie.js
under plugin folder.在插件文件夹下创建一个名为
vue-croppie.js
的文件。 And add the following to it并添加以下内容
import Vue from 'vue' import VueCroppie from 'vue-croppie' Vue.use(VueCroppie)
In your nuxt.config.js
add this under plugins在你的
nuxt.config.js
添加这个插件
{ src: '~/plugins/vue-croppie.js', ssr: false }
Now, the plugin will be available globally.现在,该插件将在全球范围内可用。 So there is no need to import and can be used directly.
所以不需要导入,直接使用即可。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.