Node.js/nuxt/全局变量

来自康健生活

引言

nuxt 自定义全局方法、全局属性、全局变量解决方案。这个是我在网上偶然找到的。

globalVariable.js
/*
 * 全局变量
 * @desc 自定义全局方法、全局属性、全局变量解决方案
 */

import Vue from 'vue'
const { host } = require('../config/env')
var comsys= {
  install(Vue){
    Vue.prototype.comsys = {
      val:function(val){
        return val
      },
      staticHost: `${host.cloudStaticHost}`
    }
  }
}
Vue.use(comsys)
nuxt.config.js
plugins: [
  { src: '~/plugins/globalVariable.js', ssr: false }
],

然后在页面直接调用 this.comsys.staticHost。ssr: false 代表代码在浏览器环境有效