forked from vuejs/v2.vuejs.org
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add in debugging recipe, delete unused categories from cookbook TOC (v…
…uejs#1464) * add in debugging recipe, delete unused categories from cookbook TOC * VSCode => VS Code * update PR based on review- title edits, clarification of sourcemaps, point to readmes
- Loading branch information
Showing
7 changed files
with
152 additions
and
29 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
--- | ||
title: Debugging in VS Code and Chrome | ||
type: cookbook | ||
order: 8 | ||
--- | ||
|
||
Every application reaches a point where it's necessary to understand failures, small to large. In this recipe, we explore a few workflows for VS Code users, who are using Chrome to test. | ||
|
||
This recipe shows how to use the [Debugger for Chrome](https://github.com/Microsoft/VSCode-chrome-debug) extension with VS Code to debug Vue.js applications generated by the [Vue CLI](https://github.com/vuejs/vue-cli). | ||
|
||
## Prerequisites | ||
|
||
You must have Chrome and VS Code installed. Make sure to get the latest version of [Debugger for Chrome](https://marketplace.visualstudio.com/items?itemName=msjsdiag.debugger-for-chrome) extension installed in VS Code. | ||
|
||
Install and create a project with the [vue-cli](https://github.com/vuejs/vue-cli), with the instructions for installation documented in the readme of the project. Change into the newly created application directory and open VS Code. | ||
|
||
### Showing Source Code in the Chrome Devtools | ||
|
||
Before you can debug your Vue components from VS Code you need to update the generated Webpack config to build sourcemaps. We do this so that our debugger has a way to map the code within a compressed file back to its position in the original file. This ensures that you can debug an application even after your assets have been optimized by Webpack. | ||
|
||
Go to `config/index.js` and find the `devtool` property. Update it to: | ||
|
||
```json | ||
devtool: 'source-map', | ||
``` | ||
|
||
### Launching the Application from VS Code | ||
|
||
Click on the Debugging icon in the Activity Bar to bring up the Debug view, then click on the gear icon to configure a launch.json file, selecting **Chrome** for the environment. Replace content of the generated launch.json with the following two configurations: | ||
|
||
![Add Chrome Configuration](/images/config_add.png) | ||
|
||
```json | ||
{ | ||
"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}/*" | ||
} | ||
} | ||
] | ||
} | ||
``` | ||
|
||
## Setting a Breakpoint | ||
|
||
1. Set a breakpoint in **src/components/HelloWorld.vue** on `line 90` where the `data` function returns a string. | ||
|
||
![Breakpoint Renderer](/images/breakpoint_set.png) | ||
|
||
2. Open your favorite terminal at the root folder and serve the app using Vue CLI: | ||
|
||
``` | ||
npm start | ||
``` | ||
|
||
3. Go to the Debug view, select the **'vuejs: chrome'** configuration, then press F5 or click the green play button. | ||
|
||
4. Your breakpoint should now be hit as the new instance of Chrome opens `http://localhost:8080`. | ||
|
||
![Breakpoint Hit](/images/breakpoint_hit.png) | ||
|
||
## Alternative Patterns | ||
|
||
### Vue Devtools | ||
|
||
There are other methods of debugging, varying in complexity. The most popular and simple of which is to use the excellent [vue-devtools](https://chrome.google.com/webstore/detail/vuejs-devtools/nhdogjmejiglipccpnnnanhbledajbpd). Some of the benefits of working with the devtools are that they enable you to live-edit data properties and see the changes reflected immediately. The other major benefit is the ability to do time travel debugging for Vuex. | ||
|
||
![Devtools Timetravel Debugger](/images/devtools-timetravel.gif) | ||
|
||
<p class="tip">Please note that if the page uses a production/minified build of Vue.js (such as the standard link from a CDN), devtools inspection is disabled by default so the Vue pane won't show up. If you switch to an unminified version, you may have to give the page a hard refresh to see them.</p> | ||
|
||
### Vuetron | ||
|
||
[Vuetron](http://vuetron.io/) is a really nice project that extends some of the work that vue-devtools has done. In addition to the normal devtools workflow, you are able to: | ||
|
||
* Quickly view API Request/Response: if you're using the fetch API for requests, this event is displayed for any request sent. The expanded card displays the request data as well as the response data. | ||
* Subscribe to specific parts of your application’s state for faster debugging | ||
* Visualize component hierarchy, and an animation allows you to collapse or expand the tree for specific hierarchy views. | ||
|
||
![Vuetron Heirarchy](/images/vuetron-heirarchy.gif) | ||
|
||
### Simple Debugger Statement | ||
|
||
The example above has a great workflow. However, there is an alternative option where you can use the [native debugger statement](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/debugger) directly in your code. If you choose to work this way, it's important that you remember to remove the statements when you're done. | ||
|
||
```js | ||
<script> | ||
export default { | ||
data() { | ||
return { | ||
message: '' | ||
} | ||
}, | ||
mounted() { | ||
const hello = 'Hello World!' | ||
debugger | ||
this.message = hello | ||
} | ||
}; | ||
</script> | ||
``` | ||
|
||
## Acknowledgements | ||
|
||
This recipe was based on a contribution from [Kenneth Auchenberg](https://twitter.com/auchenberg), [available here](https://github.com/Microsoft/VSCode-recipes/tree/master/vuejs-cli). |