🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
# uniapp引入全局变量 ## 挂载 Vue.prototype 将一些使用频率较高的常量或者方法,直接扩展到 Vue.prototype 上,每个 Vue 对象都会“继承”下来。 ### 注意这种方式只支持vue页面 示例如下: 在 main.js 中挂载属性/方法 ``` <pre class="calibre13">``` 复制代码Vue<span class="token1">.</span>prototype<span class="token1">.</span>websiteUrl <span class="token">=</span> <span class="token4">'http://uniapp.dcloud.io'</span><span class="token1">;</span> Vue<span class="token1">.</span>prototype<span class="token1">.</span>now <span class="token">=</span> Date<span class="token1">.</span>now <span class="token">||</span> <span class="token2">function</span> <span class="token1">(</span><span class="token1">)</span> <span class="token1">{</span> <span class="token2">return</span> <span class="token2">new</span> <span class="token3">Date</span><span class="token1">(</span><span class="token1">)</span><span class="token1">.</span><span class="token3">getTime</span><span class="token1">(</span><span class="token1">)</span><span class="token1">;</span> <span class="token1">}</span><span class="token1">;</span> Vue<span class="token1">.</span>prototype<span class="token1">.</span>isArray <span class="token">=</span> Array<span class="token1">.</span>isArray <span class="token">||</span> <span class="token2">function</span> <span class="token1">(</span>obj<span class="token1">)</span> <span class="token1">{</span> <span class="token2">return</span> obj <span class="token2">instanceof</span> <span class="token3">Array</span><span class="token1">;</span> <span class="token1">}</span><span class="token1">;</span> ``` ``` 然后在 pages/index/index.vue 中调用 ``` <pre class="calibre13">``` 复制代码<span class="token"><</span>script<span class="token">></span> <span class="token2">export</span> <span class="token2">default</span> <span class="token1">{</span> <span class="token3">data</span><span class="token1">(</span><span class="token1">)</span> <span class="token1">{</span> <span class="token2">return</span> <span class="token1">{</span><span class="token1">}</span><span class="token1">;</span> <span class="token1">}</span><span class="token1">,</span> <span class="token3">onLoad</span><span class="token1">(</span><span class="token1">)</span><span class="token1">{</span> console<span class="token1">.</span><span class="token3">log</span><span class="token1">(</span><span class="token4">'now:'</span> <span class="token">+</span> <span class="token2">this</span><span class="token1">.</span><span class="token3">now</span><span class="token1">(</span><span class="token1">)</span><span class="token1">)</span><span class="token1">;</span> <span class="token1">}</span><span class="token1">,</span> <span class="token">**</span># <span class="token6">/**注意用的时候要加this**/</span><span class="token">**</span> methods<span class="token1">:</span> <span class="token1">{</span> <span class="token1">}</span> <span class="token1">}</span> <span class="token"><</span><span class="token">/</span>script<span class="token">></span> ``` ``` 这种方式,只需要在 main.js 中定义好即可在每个页面中直接调用。 **Tips** - 每个页面中不要在出现重复的属性或方法名。 - 建议在 Vue.prototype 上挂载的属性或方法,可以加一个统一的前缀。比如 $url、global\_url 这样,在阅读代码时也容易与当前页面的内容区分开。