Skip to content

Commit 3d4bd33

Browse files
LinusBorgphanan
authored andcommitted
WIP: Re-order Plugins page (#1788)
* 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()
1 parent 35b875c commit 3d4bd33

File tree

2 files changed

+39
-33
lines changed

2 files changed

+39
-33
lines changed

src/v2/api/index.md

+2
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,8 @@ type: api
361361
- **Usage:**
362362

363363
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()`
364366

365367
When this method is called on the same plugin multiple times, the plugin will be installed only once.
366368

src/v2/guide/plugins.md

+37-33
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@ type: guide
44
order: 304
55
---
66

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:
108

119
1. Add some global methods or properties. e.g. [vue-custom-element](https://github.com/karol-f/vue-custom-element)
1210

@@ -18,6 +16,42 @@ Plugins usually add global-level functionality to Vue. There is no strictly defi
1816

1917
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)
2018

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+
new Vue({
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+
2155
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:
2256

2357
``` js
@@ -49,33 +83,3 @@ MyPlugin.install = function (Vue, options) {
4983
}
5084
}
5185
```
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

Comments
 (0)