You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Add in debugging recipe, delete unused categories from cookbook TOC (#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
Copy file name to clipboardExpand all lines: src/v2/cookbook/adding-instance-properties.md
+39-29Lines changed: 39 additions & 29 deletions
Original file line number
Diff line number
Diff line change
@@ -8,15 +8,15 @@ order: 2
8
8
9
9
There may be data/utilities you'd like to use in many components, but you don't want to [pollute the global scope](https://github.com/getify/You-Dont-Know-JS/blob/master/scope%20%26%20closures/ch3.md). In these cases, you can make them available to each Vue instance by defining them on the prototype:
10
10
11
-
```js
11
+
```js
12
12
Vue.prototype.$appName='My App'
13
13
```
14
14
15
15
Now `$appName` is available on all Vue instances, even before creation. If we run:
16
16
17
-
```js
17
+
```js
18
18
newVue({
19
-
beforeCreate:function() {
19
+
beforeCreate:function() {
20
20
console.log(this.$appName)
21
21
}
22
22
})
@@ -36,23 +36,23 @@ No magic is happening here. `$` is a convention Vue uses for properties that are
36
36
37
37
Another great question! If you set:
38
38
39
-
```js
39
+
```js
40
40
Vue.prototype.appName='My App'
41
41
```
42
42
43
43
Then what would you expect to be logged below?
44
44
45
-
```js
45
+
```js
46
46
newVue({
47
47
data: {
48
48
// Uh oh - appName is *also* the name of the
49
49
// instance property we defined!
50
50
appName:'The name of some other app'
51
51
},
52
-
beforeCreate:function() {
52
+
beforeCreate:function() {
53
53
console.log(this.appName)
54
54
},
55
-
created:function() {
55
+
created:function() {
56
56
console.log(this.appName)
57
57
}
58
58
})
@@ -66,7 +66,7 @@ Let's say you're replacing the [now-retired Vue Resource](https://medium.com/the
66
66
67
67
All you have to do is include axios in your project:
Note that the context binding will __not__ work if you use an ES6/2015 arrow function, as they implicitly bind to their parent scope. That means the arrow function version:
130
+
Note that the context binding will **not** work if you use an ES6/2015 arrow function, as they implicitly bind to their parent scope. That means the arrow function version:
Uncaught TypeError: Cannot read property 'split' of undefined
138
145
```
139
146
@@ -143,27 +150,30 @@ As long as you're vigilant in scoping prototype properties, using this pattern i
143
150
144
151
However, it can sometimes cause confusion with other developers. They might see `this.$http`, for example, and think, "Oh, I didn't know about this Vue feature!" Then they move to a different project and are confused when `this.$http` is undefined. Or, maybe they want to Google how to do something, but can't find results because they don't realize they're actually using Axios under an alias.
145
152
146
-
__The convenience comes at the cost of explicitness.__ When looking at a component, it's impossible to tell where `$http` came from. Vue itself? A plugin? A coworker?
153
+
**The convenience comes at the cost of explicitness.** When looking at a component, it's impossible to tell where `$http` came from. Vue itself? A plugin? A coworker?
147
154
148
155
So what are the alternatives?
149
156
150
157
## Alternative Patterns
151
158
152
159
### When Not Using a Module System
153
160
154
-
In applications with __no__ module system (e.g. via Webpack or Browserify), there's a pattern that's often used with _any_ JavaScript-enhanced frontend: a global `App` object.
161
+
In applications with **no** module system (e.g. via Webpack or Browserify), there's a pattern that's often used with _any_ JavaScript-enhanced frontend: a global `App` object.
155
162
156
163
If what you want to add has nothing to do with Vue specifically, this may be a good alternative to reach for. Here's an example:
157
164
158
-
```js
165
+
```js
159
166
var App =Object.freeze({
160
167
name:'My App',
161
168
description:'2.1.4',
162
169
helpers: {
163
170
// This is a purely functional version of
164
171
// the $reverseText method we saw earlier
165
-
reverseText:function (text) {
166
-
returntext.split('').reverse().join('')
172
+
reverseText:function(text) {
173
+
return text
174
+
.split('')
175
+
.reverse()
176
+
.join('')
167
177
}
168
178
}
169
179
})
@@ -175,7 +185,7 @@ Now the source of these shared properties is more obvious: there's an `App` obje
175
185
176
186
Another advantage is that `App` can now be used _anywhere_ in your code, whether it's Vue-related or not. That includes attaching values directly to instance options, rather than having to enter a function to access properties on `this`:
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.
8
+
9
+
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).
10
+
11
+
## Prerequisites
12
+
13
+
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.
14
+
15
+
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.
16
+
17
+
### Showing Source Code in the Chrome Devtools
18
+
19
+
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.
20
+
21
+
Go to `config/index.js` and find the `devtool` property. Update it to:
22
+
23
+
```json
24
+
devtool: 'source-map',
25
+
```
26
+
27
+
### Launching the Application from VS Code
28
+
29
+
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:
2. Open your favorite terminal at the root folder and serve the app using Vue CLI:
59
+
60
+
```
61
+
npm start
62
+
```
63
+
64
+
3. Go to the Debug view, select the **'vuejs: chrome'** configuration, then press F5 or click the green play button.
65
+
66
+
4. Your breakpoint should now be hit as the new instance of Chrome opens `http://localhost:8080`.
67
+
68
+

69
+
70
+
## Alternative Patterns
71
+
72
+
### Vue Devtools
73
+
74
+
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.
<pclass="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>
79
+
80
+
### Vuetron
81
+
82
+
[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:
83
+
84
+
* 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.
85
+
* Subscribe to specific parts of your application’s state for faster debugging
86
+
* Visualize component hierarchy, and an animation allows you to collapse or expand the tree for specific hierarchy views.
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.
93
+
94
+
```js
95
+
<script>
96
+
exportdefault {
97
+
data() {
98
+
return {
99
+
message:''
100
+
}
101
+
},
102
+
mounted() {
103
+
consthello='Hello World!'
104
+
debugger
105
+
this.message= hello
106
+
}
107
+
};
108
+
</script>
109
+
```
110
+
111
+
## Acknowledgements
112
+
113
+
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).
0 commit comments