首页 > 编程知识 正文

vue全局变量和局部变量,vue全局变量怎么定义

时间:2023-05-04 01:51:30 阅读:243115 作者:3449

只可读的 1、全局变量专用模块(vue调用时要导入,不用this)

1:新建一个global.js,声明变量,采用export将其暴露出来。如

const mobileReg = /(^1[0|1|2|3|4|5|6|7|8|9]d{9}$)|(^09d{8}$)/ //手机号,11位纯数字const passwordReg = /^(?=.*[A-Za-z])(?=.*d)[A-Za-zd]{6,15}$/ //密码,6-15位英文数字组合const chineseReg = /[u4e00-u9fa5]/ //中文const digitalReg = /^d+(.{1}d+)?$/ //数字const hostReg = /[a-zA-Z0-9][-a-zA-Z0-9]{0,62}(.[a-zA-Z0-9][-a-zA-Z0-9]{0,62}|(:[0-9]{1,4}))+.?/ //域名const nickReg = /^[A-Za-zdu2E80-u9FFF【】[]()-,,??.。;::;\、“”‘']+$/ //昵称,只能是中文/数字/英文字母 以及特殊符号const accountReg = /^(?=.*[A-Za-z])(?=.*d)[A-Za-zd]{5,20}$/ //账号,5-20位英文数字组合const emailReg = /^([a-zA-Z0-9_])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/ //邮箱function getRandColor () { var tem = Math.round(Math.random() * colorListLength) return colorList[tem]}export { mobileReg, passwordReg, chineseReg, digitalReg, hostReg, nickReg, accountReg, emailReg ,getRandColor }

调用~~.vue

import { mobileReg } from "@/utils/global"; if (!mobileReg.test(this.form.mobile)) { this.$toast("请输入正确的手机号");  return false;  } 2、全局变量模块挂载到Vue.prototype里。(调用不需要导入,要用this)   main.jsimport {loginBaseURL} from './utils/config'Vue.prototype.$baseImageUrl = baseImageUrlVue.prototype.$appName = 'My App'---.vuethis.$baseImageUrl.valuethis.$appName 可读改的

1:在main.js的data中设置

main.js定义new Vue({ router, data: function(){ return { ORDERID: 'PLDxxxxxx0001', } }, render: h => h(App)}).$mount('#app');某个vue页面// 修改this.$root.ORDERID = "xxxxx"// 引用let orderId = this.$root.ORDERID

2:Cookie的使用(js-cookie插件)

一、安装npm install js-cookie --save二、引用import Cookies from 'js-cookie'三、一般使用存到Cookie去1.Cookies.set('name', 'value'); Cookies.set('name', 'value', { expires: 7 }); Cookies.set('name', 'value', { expires: 7, path: '' }); 2.在Cookie中取出Cookies.get('name'); 3.删除Cookies.remove('name');

3:vuex(状态管理模式,data共享)

                                         -------------------------------------------------------》看下篇

 

 

 

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。