vue设置全局变量和修改
2024-08-28 21:54:54
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
最新文章
- MVC缺点
- MySQL日志管理
- mysql复制表结构及检查表、存储过程是否存在
- ZigBee协议基本介绍
- LeetCode - 204. Count Primes - 埃拉托斯特尼筛法 95.12% - (C++) - Sieve of Eratosthenes
- 神器之“c#反编译工具“
- EF 4.0 更新数据时候的一个错误及其处理
- gridview回顾
- IDFA的值什么时候会发生改变
- flex网上办(苹果)桌面系统仿真
- Confluence DotNet API发布
- 决策树系列(五)——CART
- 实验十一 团队作业7---团队项目设计完善&;编码测试
- 业务开发(一)—— MySQL
- es6新增的数组遍历方式
- bootloader研究最后一关(上)
- MATLAB小波包的分解与重构
- 求解方程A5+B5+C5+D5+E5=F5
- Perl 杂记
- [阮一峰]Linux 守护进程的启动方法
热门文章
- 实践分布式配置中心Apollo
- gitosis管理员的密钥丢失解决办法
- What is the difference between UNION and UNION ALL?
- SpringMVC(十五):Dispatcher的重要组件之一MultipartResolver(StandardServletMultipartResolver和CommonsMultipartResolver)的用法
- 带缓存的基于DateTimeFormatter的日期格式化工具类
- 【转】WPF DataGridComboBoxColumn使用
- Ajax验证
- 123456123456----updateV#%#6%#%---pinLv###1%%%----com.zzj.CarCleanGame567---前show后广--儿童洗车-222222
- HTML5 VUE单页应用 SEO 优化之 预渲染(prerender-spa-plugin)
- 小于K的两数之和