How to make debugging Vue and React code more enjoyable? The following article introduces the configuration VSCode and Su Shuang’s method of debugging Vue and React code. I hope it will be helpful to everyone!
As a front-end developer, I basically have to debug Vue/React code every day. I don’t know how everyone debugs it, but I guess there are several types:
- No debugging, just look at the code to find the problem
- Console.log print log
- Use the debugger of Chrome Devtools to debug
- Use the debugger of VSCode to debug
Different debugging methods have different efficiency and experience. Now I basically use VSCode debugger to debug, which is very efficient and has a great experience. [Recommended study: "vscode introductory tutorial"]
Maybe many students still don’t know how to use VSCode to debug web pages. I will introduce it in this article.
Let’s look at React and Vue respectively:
Use VSCode to debug React code
I used create-react-app to create a demo project with such a component:
Run the development server:
The interface displayed by the browser is like this:
How to debug it with VSCode?
We add a .vscode/launch.json configuration file in the root directory:
Created a debugging configuration, the type is chrome, and specified The debugging url is the address of the development server.
Put two breakpoints in the react code:
Then click Run:
Look, XDM, it's broken! There are call stacks, current environment variables, etc.
Release the breakpoint and continue going down.
You can also get the corresponding event object when you click:
Isn’t it super convenient!
And when you are tired of writing business and want to see the react source code, just click on a certain frame in the call stack to see:
For example, the renderWithHooks method will be called during rendering. The workInProgress object inside is the current fiber node, and its memorizedState attribute is where hooks store values:
After using VSCode to debug the React code, debug the business code or look at the source code The experience is very pleasant, no matter what.
Let’s take a look at Vue:
Use VSCode to debug Vue code
Vue debugging will be more troublesome, and you need to do some additional mapping of paths based on the above.
Because in React we write jsx and tsx directly, which corresponds to the compiled js file one-to-one, but not in Vue. In Vue, we write files in SFC (single file component) format, which requires vue-loader To separate them into different files, the paths must be mapped separately to correspond to the source code location.
That is, there are more sourceMapPathOverrides in the debugging configuration:
How to map it?
You can add a debugger to the source code and check the current mapped path in the browser:
webpack://test here -vue-debug/src/App.vue?11c4 is the path to be mapped, so where is it mapped to?
is obviously mapped to the local path, which is like this:
workspaceRoot is the environment variable provided by vscode, which is the path of the project. In this way After mapping, doesn't the address become a local file? Then the breakpoint will take effect in the local file:
Look at the path here, it is obviously mapped to the file under the project.
But when mapping, there is also a hash at the end. This hash will change. What should I do?
This path is configurable. This is actually the file path when webpack generates the sourcemap. It can be configured through output.devtoolModuleFilenameTemplate:
You can read the documentation for the available variables, so I won’t expand them here (just take a look at them):
For example, I configured the path like this:
The file path during debugging is like this:
Don’t worry about the prefix, just look at the following part. Isn’t this removed? Has it been hashed?
Then map it to the local file:
In this way, it is mapped again, and it is broken in vscode Click to take effect:
#Whether you want to debug Vue business code or want to see Vue source code, the experience will be very enjoyable.
Summary
As a front-end engineer, debugging Vue and React code is something you have to do every day. Different debugging methods have different experience and efficiency. So I want to introduce to you my commonly used method of debugging web pages with VSCode.
React debugging is relatively simple. Just add a chrome type debug configuration. Vue debugging is more troublesome. You need to do a path mapping. If there is a hash in the path, you also need to change the configuration of the generated path. Then map again (but it only needs to be configured once).
Using VSCode to debug React/Vue code is very convenient whether you are debugging business code or looking at the source code. You might as well give it a try, it will make debugging very enjoyable.
For more knowledge about VSCode, please visit: vscode tutorial! !
The above is the detailed content of How to configure VSCode, Su Shuang's debugging Vue and React code!. For more information, please follow other related articles on the PHP Chinese website!

VisualStudio is suitable for large projects and full-featured needs, while VSCode is suitable for developments that require lightweight and flexibility. 1. VisualStudio provides comprehensive IDE functions, supports multiple languages and advanced project management. 2.VSCode is known for its lightweight and scalability, and is suitable for cross-platform development and personalized configuration.

VisualStudio is suitable for large projects and development scenarios that require strong debugging capabilities, while VSCode is suitable for scenarios that require flexibility and cross-platform development. VisualStudio provides a comprehensive development environment, supports .NET development, and integrates debugging tools and project management functions. VSCode is known for its lightweight and extensibility. It supports multiple programming languages and enhances functions through plug-in systems, and is suitable for modern development processes.

Visual Studio is available in three versions: Community Free Edition is for individuals and small teams, Professional Paid Edition is for professional developers and small and medium teams, and Enterprise Ultimate Edition is for large enterprises and complex projects.

VisualStudio is highly valuable in .NET development because it is powerful and comprehensive. Despite the high cost and resource consumption, the efficiency improvement and development experience it brings is significant. Community is ideal for individual developers and small teams; large enterprises are suitable for Professional or Enterprise.

Free versions of VisualStudio include VisualStudioCommunity and VisualStudioCode. 1. VisualStudioCommunity is suitable for individual developers, open source projects and small teams. It is powerful and suitable for individual projects and learning programming. 2. VisualStudioCode is a lightweight code editor that supports multiple programming languages and extensions. It has a fast startup speed and low resource usage, making it suitable for developers who need flexibility and scalability.

The steps to install VisualStudio on Windows 8 are as follows: 1. Download the VisualStudioCommunity2019 installation package from the official Microsoft website. 2. Run the installer and select the required components. 3. It can be used after installation is completed. Be careful to select Windows 8-compatible components and make sure there is sufficient disk space and administrator rights.

VSCode can run on most modern computers as long as the basic system requirements are met: 1. Operating system: Windows 7 and above, macOS 10.9 and above, Linux; 2. Processor: 1.6GHz or faster; 3. Memory: at least 2GB RAM (4GB or higher recommended); 4. Storage space: at least 200MB of available space. By optimizing settings and reducing extended usage, you can get a smooth user experience on low-configuration computers.

To make the program run smoothly on Windows 8, the following steps are required: 1. Use compatibility mode, detect and enable this mode through code. 2. Adjust API calls and select the appropriate API according to the Windows version. 3. Perform performance optimization, try to avoid using compatibility mode, optimize API calls and use general controls.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Dreamweaver Mac version
Visual web development tools

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

SublimeText3 Linux new version
SublimeText3 Linux latest version

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.