Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 13 additions & 13 deletions src/v2/guide/plugins.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@ type: guide
order: 304
---

插件通常会为 Vue 添加全局功能。插件的范围没有限制——一般有下面几种:
插件通常用来为 Vue 添加全局功能。插件的功能范围没有严格的限制——一般有下面几种:

1. 添加全局方法或者属性如: [vue-custom-element](https://github.com/karol-f/vue-custom-element)
1. 添加全局方法或者属性如: [vue-custom-element](https://github.com/karol-f/vue-custom-element)

2. 添加全局资源:指令/过滤器/过渡等如 [vue-touch](https://github.com/vuejs/vue-touch)
2. 添加全局资源:指令/过滤器/过渡等如 [vue-touch](https://github.com/vuejs/vue-touch)

3. 通过全局 mixin 方法添加一些组件选项,如: [vue-router](https://github.com/vuejs/vue-router)
3. 通过全局混入来添加一些组件选项。如 [vue-router](https://github.com/vuejs/vue-router)

4. 添加 Vue 实例方法,通过把它们添加到 Vue.prototype 上实现。
4. 添加 Vue 实例方法,通过把它们添加到 `Vue.prototype` 上实现。

5. 一个库,提供自己的 API,同时提供上面提到的一个或多个功能如 [vue-router](https://github.com/vuejs/vue-router)
5. 一个库,提供自己的 API,同时提供上面提到的一个或多个功能如 [vue-router](https://github.com/vuejs/vue-router)

## 使用插件

Expand All @@ -25,19 +25,19 @@ order: 304
Vue.use(MyPlugin)

new Vue({
//... options
// ...组件选项
})
```

也可以传入一个选项对象
也可以传入一个可选的选项对象

``` js
Vue.use(MyPlugin, { someOption: true })
```

`Vue.use` 会自动阻止多次注册相同插件,届时只会注册一次该插件
`Vue.use` 会自动阻止多次注册相同插件,届时即使多次调用也只会注册一次该插件

Vue.js 官方提供的一些插件 (例如 `vue-router`) 在检测到 `Vue` 是可访问的全局变量时会自动调用 `Vue.use()`。然而在例如 CommonJS 的模块环境中,你应该始终显式地调用 `Vue.use()`:
Vue.js 官方提供的一些插件 (例如 `vue-router`) 在检测到 `Vue` 是可访问的全局变量时会自动调用 `Vue.use()`。然而在像 CommonJS 这样的模块环境中,你应该始终显式地调用 `Vue.use()`:

``` js
// 用 Browserify 或 webpack 提供的 CommonJS 模块环境时
Expand All @@ -48,11 +48,11 @@ var VueRouter = require('vue-router')
Vue.use(VueRouter)
```

[awesome-vue](https://github.com/vuejs/awesome-vue#components--libraries) 集合了来自社区贡献的数以千计的插件和库
[awesome-vue](https://github.com/vuejs/awesome-vue#components--libraries) 集合了大量由社区贡献的插件和库

## 开发插件

Vue.js 的插件应该有一个公开方法 `install`。这个方法的第一个参数是 `Vue` 构造器,第二个参数是一个可选的选项对象:
Vue.js 的插件应该暴露一个 `install` 方法。这个方法的第一个参数是 `Vue` 构造器,第二个参数是一个可选的选项对象:

``` js
MyPlugin.install = function (Vue, options) {
Expand All @@ -69,7 +69,7 @@ MyPlugin.install = function (Vue, options) {
...
})

// 3. 注入组件
// 3. 注入组件选项
Vue.mixin({
created: function () {
// 逻辑...
Expand Down