🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
[TOC] # DevTools ### Vue has a great panel that integrates into the Browser Developer Tools, which lets you inspect your application and interact with it, to ease debugging and understanding - [Install on Chrome](#install-on-chrome) - [Install on Firefox](#install-on-firefox) - [Install the standalone app](#install-the-standalone-app) - [How to use the Developer Tools](#how-to-use-the-developer-tools) - [Filter components](#filter-components) - [Select component in the page](#select-component-in-the-page) - [Format components names](#format-components-names) - [Filter inspected data](#filter-inspected-data) - [Inspect DOM](#inspect-dom) - [Open in editor](#open-in-editor) - - - - - - When you're first experimenting with Vue, if you open the Browser Developer Tools you will find this message: "*Download the Vue Devtools extension for a better development experience: <https://github.com/vuejs/vue-devtools>*" ![](https://img.kancloud.cn/2a/a9/2aa9977b2a96df23be8b679aae46fde2_1040x510.png) This is a friendly reminder to install the **Vue Devtools Extension**. What's that? Any popular framework has its own devtools extension, which generally adds a new panel to the browser developer tools that is much more specialized than the ones that the browser ships by default. In this case, the panel will let us inspect our Vue application and interact with it. This tool will be an amazing help when building Vue apps. The developer tools can only inspect a Vue application when it's in development mode. This makes sure no one can use them to interact with your production app (and will make Vue more performant because it does not have to care about the devtools) Let's install it! There are 3 ways to install the Vue Dev Tools: - on Chrome - on Firefox - as a standalone application Safari, Edge and other browsers are not supported with a custom extension, but using the standalone application you can debug a Vue.js app running in any browser. ## Install on Chrome Go to this page on the Google Chrome Store: <https://chrome.google.com/webstore/detail/vuejs-devtools/nhdogjmejiglipccpnnnanhbledajbpd> and click **Add to Chrome**. ![](https://img.kancloud.cn/5b/42/5b420b928f46cae140d4d8ee428e1790_1225x881.png) Go through the installation process: ![](https://img.kancloud.cn/52/38/5238d215a1fc68320757804c16d0ec3a_1225x881.png) The Vue.js devtools icon shows up in the toolbar. If the page does not have a Vue.js instance running, it's grayed out. ![](https://img.kancloud.cn/dd/a2/dda2913f7633a4201baa1c91915796d7_1225x881.png) If Vue.js is detected, the icon has the Vue logo colors. ![](https://img.kancloud.cn/f2/f0/f2f0c00e00d412355147a73746d3425f_1225x848.png) The icon does nothing except showing us that there *is* a Vue.js instance. To use the devtools, we must open the Developer Tools panel, using "View → Developer → Developer Tools", or `Cmd-Alt-i` ![](https://img.kancloud.cn/ec/8c/ec8c46dd04efeec1466893771896663e_1072x664.png) ## Install on Firefox You can find the Firefox dev tools extension in the Mozilla addons store: <https://addons.mozilla.org/en-US/firefox/addon/vue-js-devtools/> ![](https://img.kancloud.cn/e1/94/e194ddf1a269c02c7460111e68c0e2f4_1060x806.png) Click "**Add to Firefox**" and the extension will be installed. As with Chrome, a grayed icon shows up in the toolbar ![](https://img.kancloud.cn/c7/00/c7001b0500492252239a4fa96fbdacdb_1060x806.png) And when you visit a site that has a Vue instance running, it will become green, and when we open the Dev Tools we will see a "**Vue**" panel: ![](https://img.kancloud.cn/9c/23/9c23326ffb5397991f4b57a72104f0d0_1012x669.png) ## Install the standalone app Alternatively, you can use the DevTools standalone app. Simply install it using ``` npm install -g @vue/devtools //or yarn global add @vue/devtools ``` and run it by calling ``` vue-devtools ``` This will open the standalone Electron-based application. ![](https://img.kancloud.cn/2c/ed/2cedefbc7a0e95bb3ef724c9ffe3bc05_912x712.png) now, paste the script tag it shows you: ``` <script src="http://localhost:8098"></script> ``` inside the project `index.html` file, and wait for the app to be reloaded, and it will automatically connect to the app: ![](https://img.kancloud.cn/3c/39/3c3930f3ae3fb247faf5e14a8e07588e_912x712.png) ## How to use the Developer Tools As mentioned, the Vue DevTools can be activated by opening the Developer Tools in the browser and moving to the Vue panel. Another option is to right-click on any element in the page, and choose "Inspect Vue component": ![](https://img.kancloud.cn/c6/9f/c69fe6be82759f05c2584e7b7eb62cfc_796x524.png) When the Vue DevTools panel is open, we can navigate the components tree. When we choose a component from the list on the left, the right panel shows the props and data it holds: ![](https://img.kancloud.cn/1d/45/1d456d6bbe55a45146b7f6e9b92b4371_1072x664.png) On the top there are 4 buttons: - **Components** (the current panel), which lists all the component instances running in the current page. Vue can have multiple instances running at the same time, for example it might manage your shopping cart widget and the slideshow, with separate, lightweight apps. - **Vuex** is where you can inspect the state managed through [Vuex](../vuex). - **Events** shows all the events emitted - **Refresh** reloads the devtools panel Notice the small `= $vm0` text beside a component? It's a handy way to inspect a component using the Console. Pressing the "esc" key shows up the console in the bottom of the devtools, and you can type `$vm0` to access the Vue component: ![](https://img.kancloud.cn/cb/fa/cbfa817c1fe4c28b724a0467ff8e3783_1072x664.png) This is very cool to inspect and interact with components without having to assign them to a global variable in the code. ### Filter components Start typing a component name, and the components tree will filter out the ones that don't match. ![](https://img.kancloud.cn/0d/d4/0dd440d0b6f6a88750690c07c6beadb4_1072x505.png) ### Select component in the page Click the ![](https://img.kancloud.cn/e9/c5/e9c55ad0093c172dc50d672dfc6b2617_48x40.png) button and you can hover any component in the page with the mouse, click it, and it will be opened in the devtools. ### Format components names You can choose to show components in camelCase or use dashes. ### Filter inspected data On the right panel, you can type any word to filter the properties that don't match it. ### Inspect DOM Click the Inspect DOM button to be brought to the DevTools Elements inspector, with the DOM element generated by the component: ![](https://img.kancloud.cn/9f/f3/9ff3e8983e3efb172610d100fb828adc_1066x512.png) ### Open in editor Any user component (not framework-level components) has a button that opens it in your default editor. Very handy.