TS2307:找不到模块“./App.vue”或其相应的类型声明

2024-03-25

我想使用 typescript + Vue 3 开发 google chrome 扩展。在谷歌浏览器扩展弹出索引中,打字稿代码index.ts好像:

import { createApp } from 'vue'
import App from './App.vue'

document.addEventListener('DOMContentLoaded', () => {
  createApp(App)
    .mount('#app')
})

and the App.vue好像:

<template lang="pug">
.container
  h1 {{ title }}
  hr
  Options
  hr
  Counter
  hr
  Memos
</template>

<script lang="ts">
import { defineComponent } from 'vue'
import Options from './components/Options.vue'
export default defineComponent({
  setup() {
    const title = process.env.APP_NAME
    return {
      title
    }
  },
  components: {
    Options,
  }
})
</script>

<style lang="scss" scoped>
div.container {
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  h1 {
    margin: 0px;
    padding: 5px 20px;
    font-size: 1.5em;
    font-weight: lighter;
    white-space: nowrap;
  }
  hr {
    size: 1px;
    width: 100%;
    color: gray;
    margin: 0px;
  }
}
</style>

当我运行此代码时,显示如下错误:

➜  reddwaf-translate-plugin git:(main) ✗ npm run dev

> [email protected] /cdn-cgi/l/email-protection dev
> rm -rf src/bundle && webpack --mode development --config src/resource/config/webpack.dev.config.js

assets by path popup/ 1.47 KiB
  asset popup/popup.js 1.27 KiB [emitted] (name: popup/popup) 1 related asset
  asset popup/popup.html 201 bytes [emitted]
asset resource/image/logo.png 7.14 KiB [emitted] [from: src/resource/image/logo.png] [copied]
asset manifest.json 1.22 KiB [emitted] [from: src/manifest.json] [copied]
./src/popup/index.ts 39 bytes [built] [code generated] [1 error]

ERROR in ./src/popup/index.ts
Module build failed (from ./node_modules/ts-loader/index.js):
Error: TypeScript emitted no output for /frontend/reddwaf-translate-plugin/src/popup/index.ts.
    at makeSourceMapAndFinish (/frontend/reddwaf-translate-plugin/node_modules/ts-loader/dist/index.js:52:18)
    at successLoader (/frontend/reddwaf-translate-plugin/node_modules/ts-loader/dist/index.js:39:5)
    at Object.loader (/frontend/reddwaf-translate-plugin/node_modules/ts-loader/dist/index.js:22:5)

ERROR in /frontend/reddwaf-translate-plugin/src/popup/index.ts
./src/popup/index.ts 2:16-27
[tsl] ERROR in /frontend/reddwaf-translate-plugin/src/popup/index.ts(2,17)
      TS2307: Cannot find module './App.vue' or its corresponding type declarations.
ts-loader-default_0c5a263502dc9404

webpack 5.67.0 compiled with 2 errors in 955 ms

这是我的package.json:

{
  "name": "reddwaf-translate-plugin",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "dev": "rm -rf src/bundle && webpack --mode development --config src/resource/config/webpack.dev.config.js",
    "build": "gulp --cwd . --gulpfile build/gulp-build.js",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "repository": {
    "type": "git",
    "url": "git+https://github.com/jiangxiaoqiang/reddwaf-translate-plugin.git"
  },
  "author": "",
  "license": "ISC",
  "bugs": {
    "url": "https://github.com/jiangxiaoqiang/reddwaf-translate-plugin/issues"
  },
  "homepage": "https://github.com/jiangxiaoqiang/reddwaf-translate-plugin#readme",
  "devDependencies": {
    "@babel/core": "^7.16.12",
    "@babel/preset-env": "^7.16.11",
    "babel-loader": "^8.2.3",
    "copy-webpack-plugin": "^10.2.1",
    "html-webpack-plugin": "^5.5.0",
    "mini-css-extract-plugin": "^2.5.3",
    "ts-loader": "^9.2.6",
    "typescript": "^4.5.5",
    "webpack": "^5.67.0",
    "webpack-cli": "^4.9.2"
  },
  "dependencies": {
    "@types/chrome": "^0.0.177",
    "@types/webextension-polyfill": "^0.8.2",
    "vue": "^3.2.29",
    "vue-loader": "^15.9.8",
    "vuex": "^4.0.2"
  }
}

这是 webpack 配置:

const path = require('path');
const webpack = require( 'webpack' );
const MiniCssExtractPlugin = require( 'mini-css-extract-plugin');
const HtmlWebpackPlugin = require( 'html-webpack-plugin');
const CopyPlugin = require("copy-webpack-plugin");

module.exports = {
  entry : {
    'popup/popup' : './src/popup/' 
  } ,
  resolve: {
    extensions: ['.tsx', '.ts', '.js'],
    alias: {
        vue: 'vue/dist/vue.esm-bundler.js'
    },
  },
  output : {
    path : path.resolve(__dirname, '../../bundle') ,
    filename : '[name].js'
  },
  module : {
    rules : [
      {
        test: /\.vue$/,
        loader: 'vue-loader'
      },
      {
        test: /\.ts$/,
        loader: 'ts-loader',
        options: {
          appendTsSuffixTo: [/\.vue$/]
        },
        exclude: /node_modules/
      },
      
      {
        test : /\.js$/ ,
        exclude : [ /node_modules(?!(\/|\\?\\)(translation\.js|selection-widget|connect\.io|chrome-env)\1)/ ] ,
        loader : 'babel-loader'
      } ,
      {
        test: /\.css$/i,
        use: [MiniCssExtractPlugin.loader, "css-loader"],
      },
      {
        test : /\.(scss)$/ ,
        use: [MiniCssExtractPlugin.loader, 'css-loader', 'sass-loader']
      }
    ]
  },
  plugins : [
    new CopyPlugin({
      patterns: [
        { from: "src/manifest.json", to: "manifest.json" },
        { from: "src/resource/image", to: "resource/image" },
      ],
    }),
    new MiniCssExtractPlugin({
      filename: "[name].css",
      chunkFilename: "[id].css",
    }),
    new HtmlWebpackPlugin({
      filename: 'popup/popup.html',
      template: 'src/popup/index.html'
    }),
    new webpack.DefinePlugin({
      __VUE_OPTIONS_API__: false,
      __VUE_PROD_DEVTOOLS__: false,
    }),
  ]
};

尝试将以下内容放入src/shims-vue.d.ts file:

declare module '*.vue' {
  import type { DefineComponent } from 'vue'
  const component: DefineComponent<{}, {}, any>
  export default component
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

TS2307:找不到模块“./App.vue”或其相应的类型声明 的相关文章

  • 使用 Object.entries() 时保留键类型

    这是我的问题的一个例子 ts 游乐场演示 https www typescriptlang org play code C4TwDgpgBAKg1hEUC8UDkAzA9ltUA 6ARgIYBOehaAJhBiQK4A2wA0omgFCc
  • 从版本 14 更新到 15 后,Angular $localize 错误

    从版本更新我们的 Angular 应用程序后14 0 4到版本15 1 3 之前也尝试过15 1 2 我们在尝试访问该应用程序时收到以下错误 Uncaught Error It looks like your application or
  • 如何在 .tsx 打字稿中包含 .css 文件?

    如何在 tsx 中包含 css 文件以及如何使用它 即我如何渲染静态文件 import as React from react import Header from header import home css export class H
  • QUnit 不会运行测试

    我刚刚开始使用 QUnit 并遇到问题 我目前正在使用 TypeScript 它是一个 JavaScript 编译器 我在与我的主要课程结构平行的课程中进行测试 在每个类中 我都有一个名为 runTests 的函数 为了执行这些测试 我循环
  • 使用字符串枚举来扩展 keyof

    我在理解使用字符串枚举来索引类型时的行为时遇到了一些困难 似乎有时 TS 会识别字符串枚举的值是keyof某些类型 有时则不然 为了显示 enum Key FOO foo type MyObj foo string 因此 类型属性的枚举 即
  • React Typescript:从文件输入获取文件

    我得到的错误是Property files does not exist on type ChangeEvent
  • If 语句中的 Typescript 类型推断不能用作变量

    考虑以下示例 class A class B extends A foo return 6 const variable A new B const isValid variable instanceof B if isValid vari
  • Angular2:setTimeout仅调用一次

    我正在 Angular2 中实现需要使用的功能setTimeout My code public ngAfterViewInit void this authenticate loop private authenticate loop s
  • Svelte:无法识别导入的 TypeScript 文件

    我正在尝试使用 Rollup 使用 Svelte 和 TypeScript 构建一个应用程序 当我尝试构建我的 Svelte 组件时 我似乎无法让它编译我的 ts包含的文件 svelte成分 我不断收到此错误 Error Unexpecte
  • Angular 中的文件输入事件类型

    所以我已经使用 Angular 和 Typescript 很长时间了 我似乎无法找出输入文件的类型是什么 例如
  • 未强制执行 Typescript 抽象类静态方法

    我在 TypeScript 中有这个简单的代码 abstract class Config readonly NAME string readonly TITLE string static CoreInterface gt any cla
  • 想要动态处理与分页相关的页码显示:ReactJS

    我有一些分页逻辑工作得很好 唯一的问题是我只能让它显示并固定数量的页面可供选择 现在我已经把它放到了 5 页 但我希望它能够根据总记录动态更改 假设我有 100 条记录 每页限制为 10 条 将有 10 页 现在我只能让它以这种方式显示 第
  • 从回调中访问状态

    我在从回调访问组件状态时遇到问题 国家的价值num更改正确 但此类更改对于加载时定义的回调函数不可见 import React useState from react class MyObject callback gt void cons
  • Webpack 开发服务器重新加载在虚拟机上不起作用

    我正在使用 vagrant over mac OSX 在 Ubuntu 15 10 的虚拟机上运行 webpack 服务器 webpack 配置非常干净 var HtmlWebpackPlugin require html webpack
  • Webpack 4:如何使用 LESS 获取 CSS 源映射?

    多年来我一直在尝试让 CSS 源映射在 webpack 中工作 但没有成功 我不确定链条中哪里出了问题 我希望有人能指出我正确的方向 这是发生的事情 行号是错误的 实际上文件名也是错误的 main less只是包含一堆 import也就是说
  • 在 Webpack 中的不同配置导出上应用 CommonsChunkPlugin?

    我正在开发一个由以下 实体 组成的项目 静态网站的几个页面 An app 管理仪表板 在我最初的webpack config我将每个实体作为不同的实体进行处理的设置entry点 例如 我会有类似的东西 entry vendor jquery
  • 在 Angular 单个组件中导入第 3 方 javascript 库

    我目前正在开发 Angular4 应用程序 我想import some JavaScript 库 but 仅针对单个组件 目前我可以通过在里面定义它们的路径来轻松导入这个库 angular cli json像那样 scripts node
  • Angular 2 - ng 构建与 webpack 构建

    我想了解构建和部署 Angular 2 Web 应用程序的最佳方法是什么 我最终需要将其作为 Web 捆绑资源提供给我的 dropwizard 应用程序 我试图了解是否应该保留 ng build 并使用它来生成我的 dist 文件夹 或者我
  • 如何获取数组作为 GraphQL 解析器的输入

    我想得到一个字符串数组ids查询变量中的参数并在我的解析器中使用它 下面是我的代码 People resolver ts import Resolver Query Mutation Args from nestjs graphql imp
  • Eslint errorring 导入没有扩展名的 jsx

    我正在尝试在 es6 中导入 jsx 文件而不需要 jsx 扩展名 import LoginErrorDialog from LoginErrorDialogView Not import LoginErrorDialog from Log

随机推荐