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
* Re-order Plugins page
- First explain what plugins are and why kinds of Plugins exist
- Then explain how to use them.
- Finally, for the "advanced users", explain how to write them.
* add note about when to call Vue.use()
Copy file name to clipboardExpand all lines: src/v2/api/index.md
+2
Original file line number
Diff line number
Diff line change
@@ -361,6 +361,8 @@ type: api
361
361
-**Usage:**
362
362
363
363
Install a Vue.js plugin. If the plugin is an Object, it must expose an `install` method. If it is a function itself, it will be treated as the install method. The install method will be called with Vue as the argument.
364
+
365
+
This method has to be called before calling `new Vue()`
364
366
365
367
When this method is called on the same plugin multiple times, the plugin will be installed only once.
Copy file name to clipboardExpand all lines: src/v2/guide/plugins.md
+37-33
Original file line number
Diff line number
Diff line change
@@ -4,9 +4,7 @@ type: guide
4
4
order: 304
5
5
---
6
6
7
-
## Writing a Plugin
8
-
9
-
Plugins usually add global-level functionality to Vue. There is no strictly defined scope for a plugin - there are typically several types of plugins you can write:
7
+
Plugins usually add global-level functionality to Vue. There is no strictly defined scope for a plugin - there are typically several types of plugins:
10
8
11
9
1. Add some global methods or properties. e.g. [vue-custom-element](https://github.com/karol-f/vue-custom-element)
12
10
@@ -18,6 +16,42 @@ Plugins usually add global-level functionality to Vue. There is no strictly defi
18
16
19
17
5. A library that provides an API of its own, while at the same time injecting some combination of the above. e.g. [vue-router](https://github.com/vuejs/vue-router)
20
18
19
+
## Using a Plugin
20
+
21
+
Use plugins by calling the `Vue.use()` global method. This has to be done before you start your app by calling `new Vue()`:
22
+
23
+
```js
24
+
// calls `MyPlugin.install(Vue)`
25
+
Vue.use(MyPlugin)
26
+
27
+
newVue({
28
+
//... options
29
+
})
30
+
```
31
+
32
+
You can optionally pass in some options:
33
+
34
+
```js
35
+
Vue.use(MyPlugin, { someOption:true })
36
+
```
37
+
38
+
`Vue.use` automatically prevents you from using the same plugin more than once, so calling it multiple times on the same plugin will install the plugin only once.
39
+
40
+
Some plugins provided by Vue.js official plugins such as `vue-router` automatically calls `Vue.use()` if `Vue` is available as a global variable. However in a module environment such as CommonJS, you always need to call `Vue.use()` explicitly:
41
+
42
+
```js
43
+
// When using CommonJS via Browserify or Webpack
44
+
var Vue =require('vue')
45
+
var VueRouter =require('vue-router')
46
+
47
+
// Don't forget to call this
48
+
Vue.use(VueRouter)
49
+
```
50
+
51
+
Checkout [awesome-vue](https://github.com/vuejs/awesome-vue#components--libraries) for a huge collection of community-contributed plugins and libraries.
52
+
53
+
## Writing a Plugin
54
+
21
55
A Vue.js plugin should expose an `install` method. The method will be called with the `Vue` constructor as the first argument, along with possible options:
22
56
23
57
```js
@@ -49,33 +83,3 @@ MyPlugin.install = function (Vue, options) {
49
83
}
50
84
}
51
85
```
52
-
53
-
## Using a Plugin
54
-
55
-
Use plugins by calling the `Vue.use()` global method:
56
-
57
-
```js
58
-
// calls `MyPlugin.install(Vue)`
59
-
Vue.use(MyPlugin)
60
-
```
61
-
62
-
You can optionally pass in some options:
63
-
64
-
```js
65
-
Vue.use(MyPlugin, { someOption:true })
66
-
```
67
-
68
-
`Vue.use` automatically prevents you from using the same plugin more than once, so calling it multiple times on the same plugin will install the plugin only once.
69
-
70
-
Some plugins provided by Vue.js official plugins such as `vue-router` automatically calls `Vue.use()` if `Vue` is available as a global variable. However in a module environment such as CommonJS, you always need to call `Vue.use()` explicitly:
71
-
72
-
```js
73
-
// When using CommonJS via Browserify or Webpack
74
-
var Vue =require('vue')
75
-
var VueRouter =require('vue-router')
76
-
77
-
// Don't forget to call this
78
-
Vue.use(VueRouter)
79
-
```
80
-
81
-
Checkout [awesome-vue](https://github.com/vuejs/awesome-vue#components--libraries) for a huge collection of community-contributed plugins and libraries.
0 commit comments