1. 只读的全局变量

对于只读的全局变量,知道的有以下两种使用方式:

1)global.js 模块中定义;其他模块import后再使用即可

1.1)定义

import Vue from 'vue';

let MyComm = new Vue({
methods: {
deleteCookie: function (cname) {
let d = new Date();
let expires = "expires=" + d.toGMTString();
document.cookie = cname + "=; " + expires;
}
}) export default MyComm;

1.2)引用

import MyComm from "./components/common/comm";
MyComm.deleteCookie('ms_username')

2)gobal.js 模块中定义,并绑定到 prototype,其他任何Vue实例可直接引用 this.$xxxx

2.1)定义,绑定&引用

import Vue from 'vue';

let MyComm = new Vue({
methods: {
deleteCookie: function (cname) {
let d = new Date();
let expires = "expires=" + d.toGMTString();
document.cookie = cname + "=; " + expires;
}
}) export default MyComm;
Vue.prototype.$MyComm = MyComm;
//项目中任何地方都可如此引用
this.$MyComm.deleteCookie('ms_username')

2.可读写的全局变量

如果想随时修改全局变量的值,有一种办法:main.js中data定义,其他地方通过 this.$root.{paramName} 来引用/修改

2.1)main.js 中定义

new Vue({
router,
data: function(){
return {
URL: 'http://localhost:8080',
}
},
render: h => h(App)
}).$mount('#app');

2.2)引用

// 修改
this.$root.URL= "xxxxx" // 引用
let URL = this.$root.URL

最新文章

  1. MVC缺点
  2. MySQL日志管理
  3. mysql复制表结构及检查表、存储过程是否存在
  4. ZigBee协议基本介绍
  5. LeetCode - 204. Count Primes - 埃拉托斯特尼筛法 95.12% - (C++) - Sieve of Eratosthenes
  6. 神器之“c#反编译工具“
  7. EF 4.0 更新数据时候的一个错误及其处理
  8. gridview回顾
  9. IDFA的值什么时候会发生改变
  10. flex网上办(苹果)桌面系统仿真
  11. Confluence DotNet API发布
  12. 决策树系列(五)——CART
  13. 实验十一 团队作业7---团队项目设计完善&编码测试
  14. 业务开发(一)—— MySQL
  15. es6新增的数组遍历方式
  16. bootloader研究最后一关(上)
  17. MATLAB小波包的分解与重构
  18. 求解方程A5+B5+C5+D5+E5=F5
  19. Perl 杂记
  20. [阮一峰]Linux 守护进程的启动方法

热门文章

  1. 实践分布式配置中心Apollo
  2. gitosis管理员的密钥丢失解决办法
  3. What is the difference between UNION and UNION ALL?
  4. SpringMVC(十五):Dispatcher的重要组件之一MultipartResolver(StandardServletMultipartResolver和CommonsMultipartResolver)的用法
  5. 带缓存的基于DateTimeFormatter的日期格式化工具类
  6. 【转】WPF DataGridComboBoxColumn使用
  7. Ajax验证
  8. 123456123456----updateV#%#6%#%---pinLv###1%%%----com.zzj.CarCleanGame567---前show后广--儿童洗车-222222
  9. HTML5 VUE单页应用 SEO 优化之 预渲染(prerender-spa-plugin)
  10. 小于K的两数之和