mirror of
https://github.com/fofolee/uTools-Manuals.git
synced 2025-06-09 23:44:06 +08:00
86 lines
5.0 KiB
HTML
86 lines
5.0 KiB
HTML
<div class="content guide with-sidebar plugins-guide">
|
||
<h1>插件</h1>
|
||
<p>插件通常用来为 Vue 添加全局功能。插件的功能范围没有严格的限制——一般有下面几种:</p>
|
||
<ol>
|
||
<li><p>添加全局方法或者属性。如: <a href="https://github.com/karol-f/vue-custom-element" rel="noopener" target="_blank">vue-custom-element</a></p>
|
||
</li>
|
||
<li><p>添加全局资源:指令/过滤器/过渡等。如 <a href="https://github.com/vuejs/vue-touch" rel="noopener" target="_blank">vue-touch</a></p>
|
||
</li>
|
||
<li><p>通过全局混入来添加一些组件选项。如 <a href="https://github.com/vuejs/vue-router" rel="noopener" target="_blank">vue-router</a></p>
|
||
</li>
|
||
<li><p>添加 Vue 实例方法,通过把它们添加到 <code>Vue.prototype</code> 上实现。</p>
|
||
</li>
|
||
<li><p>一个库,提供自己的 API,同时提供上面提到的一个或多个功能。如 <a href="https://github.com/vuejs/vue-router" rel="noopener" target="_blank">vue-router</a></p>
|
||
</li>
|
||
</ol>
|
||
<h2 id="使用插件"><a class="headerlink" href="#使用插件" title="使用插件"></a>使用插件</h2><p>通过全局方法 <code>Vue.use()</code> 使用插件。它需要在你调用 <code>new Vue()</code> 启动应用之前完成:</p>
|
||
<pre><code class="hljs js"><span class="hljs-comment">// 调用 `MyPlugin.install(Vue)`</span>
|
||
Vue.use(MyPlugin)
|
||
|
||
<span class="hljs-keyword">new</span> Vue({
|
||
<span class="hljs-comment">// ...组件选项</span>
|
||
})</code></pre>
|
||
<p>也可以传入一个可选的选项对象:</p>
|
||
<pre><code class="hljs js">Vue.use(MyPlugin, { <span class="hljs-attr">someOption</span>: <span class="hljs-literal">true</span> })</code></pre>
|
||
<p><code>Vue.use</code> 会自动阻止多次注册相同插件,届时即使多次调用也只会注册一次该插件。</p>
|
||
<p>Vue.js 官方提供的一些插件 (例如 <code>vue-router</code>) 在检测到 <code>Vue</code> 是可访问的全局变量时会自动调用 <code>Vue.use()</code>。然而在像 CommonJS 这样的模块环境中,你应该始终显式地调用 <code>Vue.use()</code>:</p>
|
||
<pre><code class="hljs js"><span class="hljs-comment">// 用 Browserify 或 webpack 提供的 CommonJS 模块环境时</span>
|
||
<span class="hljs-keyword">var</span> Vue = <span class="hljs-built_in">require</span>(<span class="hljs-string">'vue'</span>)
|
||
<span class="hljs-keyword">var</span> VueRouter = <span class="hljs-built_in">require</span>(<span class="hljs-string">'vue-router'</span>)
|
||
|
||
<span class="hljs-comment">// 不要忘了调用此方法</span>
|
||
Vue.use(VueRouter)</code></pre>
|
||
<p><a href="https://github.com/vuejs/awesome-vue#components--libraries" rel="noopener" target="_blank">awesome-vue</a> 集合了大量由社区贡献的插件和库。</p>
|
||
<h2 id="开发插件"><a class="headerlink" href="#开发插件" title="开发插件"></a>开发插件</h2><p>Vue.js 的插件应该暴露一个 <code>install</code> 方法。这个方法的第一个参数是 <code>Vue</code> 构造器,第二个参数是一个可选的选项对象:</p>
|
||
<pre><code class="hljs js">MyPlugin.install = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">Vue, options</span>) </span>{
|
||
<span class="hljs-comment">// 1. 添加全局方法或属性</span>
|
||
Vue.myGlobalMethod = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>{
|
||
<span class="hljs-comment">// 逻辑...</span>
|
||
}
|
||
|
||
<span class="hljs-comment">// 2. 添加全局资源</span>
|
||
Vue.directive(<span class="hljs-string">'my-directive'</span>, {
|
||
bind (el, binding, vnode, oldVnode) {
|
||
<span class="hljs-comment">// 逻辑...</span>
|
||
}
|
||
...
|
||
})
|
||
|
||
<span class="hljs-comment">// 3. 注入组件选项</span>
|
||
Vue.mixin({
|
||
<span class="hljs-attr">created</span>: <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>{
|
||
<span class="hljs-comment">// 逻辑...</span>
|
||
}
|
||
...
|
||
})
|
||
|
||
<span class="hljs-comment">// 4. 添加实例方法</span>
|
||
Vue.prototype.$myMethod = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">methodOptions</span>) </span>{
|
||
<span class="hljs-comment">// 逻辑...</span>
|
||
}
|
||
}</code></pre>
|
||
<div class="guide-links">
|
||
<span>← <a href="render-function.html">渲染函数 & JSX</a></span>
|
||
<span style="float: right;"><a href="filters.html">过滤器</a> →</span>
|
||
</div>
|
||
<div class="footer">
|
||
<script src="//m.servedby-buysellads.com/monetization.js" type="text/javascript"></script>
|
||
<div class="bsa-cpc"></div>
|
||
<script>
|
||
(function(){
|
||
if(typeof _bsa !== 'undefined' && _bsa) {
|
||
_bsa.init('default', 'CKYD62QM', 'placement:vuejsorg', {
|
||
target: '.bsa-cpc',
|
||
align: 'horizontal',
|
||
disable_css: 'true'
|
||
});
|
||
}
|
||
})();
|
||
</script>
|
||
|
||
发现错误?想参与编辑?
|
||
<a href="https://github.com/vuejs/cn.vuejs.org/blob/master/srcplugins.md" target="_blank">
|
||
在 GitHub 上编辑此页!
|
||
</a>
|
||
</div>
|
||
</div> |