gpt4 book ai didi

typescript - 在 VS Code 中调试不适用于 Typescript Vue 应用程序

转载 作者:行者123 更新时间:2023-12-05 08:22:17 31 4
gpt4 key购买 nike

我像这样生成了一个开箱即用的新 Vue 应用程序:

? Please pick a preset: Manually select features
? Check the features needed for your project: Choose Vue version, Babel, TS, PWA, Router, Vuex, CSS Pre-processors, Linter, Unit, E2E
? Choose a version of Vue.js that you want to start the project with 3.x (Preview)
? Use class-style component syntax? Yes
? Use Babel alongside TypeScript (required for modern mode, auto-detected polyfills, transpiling JSX)? Yes
? Use history mode for router? (Requires proper server setup for index fallback in production) Yes
? Pick a CSS pre-processor (PostCSS, Autoprefixer and CSS Modules are supported by default): Sass/SCSS (with dart-sass)
? Pick a linter / formatter config: Prettier
? Pick additional lint features: Lint on save, Lint and fix on commit
? Pick a unit testing solution: Jest
? Pick an E2E testing solution: WebdriverIO
? Pick browsers to run end-to-end test on Chrome
? Where do you prefer placing config for Babel, ESLint, etc.? In dedicated config files
? Save this as a preset for future projects? No

像这样将 launch.json 添加到 VS Code:

{
"version": "0.2.0",
"configurations": [
{
"type": "chrome",
"request": "launch",
"name": "vuejs: chrome",
"url": "http://localhost:8080",
"webRoot": "${workspaceFolder}/src",
"breakOnLoad": true,
"sourceMapPathOverrides": {
"webpack:///src/*": "${webRoot}/*"
}
}
]
}

像这样将 vue.config.js 添加到我的项目根目录中:

module.exports = {
configureWebpack: {
devtool: 'source-map'
}
}

在 main.ts 中放置一个断点:

createApp(App)
.use(store)
.use(router)
.mount("#app");

从命令行运行 npm run serve 并在 VS Code 中按 F5。没有休息。我该如何解决这个问题? Typescript 只是不适合 Vue 吗?我以前做过 JS 项目。

最佳答案

不幸的是,其他答案都不适合我。可能是因为我使用的是单文件组件 (SFC) 而它们不是?

在花了几个小时深入研究这个问题之后,我想我已经找到它的根源了:

在构建过程中,Vue 会将您的 SFC 拆分为各自的部分(<template><script><style>)并通过适当的加载器运行这些部分。

当您的 SFC 使用 TypeScript 时,<script>这些 SFC 的一部分是使用 TypeScript 编译器 (TSC) 编译的。问题是 TSC 不知道它正在编译的代码实际上源自您的 SFC,因为它看到的只是来自 <script> 的内容。 之后部分已被 Vue 编译器隔离。

因此,TSC 生成的源映射包含不正确的行号(它们没有向下移动以补偿 SFC 的 <template> 部分)、不正确的源文件引用(文件名包含由Vue 编译器)和不正确的源文件内容(内容将包含<script>代码,并且将缺少<template><style>代码)。

不幸的是,我无法在这里描述如何解决这个问题,因为这会使这篇文章很长,但我可以将您重定向到blog post。我写了关于这个主题。

简而言之,我解决问题的方法是利用 Webpack 构建过程来更正损坏的 Source Maps。

关于typescript - 在 VS Code 中调试不适用于 Typescript Vue 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63972838/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com