vue设置全局变量和修改
生活随笔
收集整理的這篇文章主要介紹了
vue设置全局变量和修改
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
?
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實(shí)例可直接引用 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; //項(xiàng)目中任何地方都可如此引用 this.$MyComm.deleteCookie('ms_username')?
2.可讀寫的全局變量?
如果想隨時(shí)修改全局變量的值,有一種辦法: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
?
轉(zhuǎn)載于:https://www.cnblogs.com/weibanggang/p/11368615.html
總結(jié)
以上是生活随笔為你收集整理的vue设置全局变量和修改的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: mke2fs和mkfs命令使用
- 下一篇: android listview 禁止滚