Vue进阶(贰幺零):Vue 全局函数、组件、变量挂载方式_vue main

const startDate = new Date(new Date() - 24 * 60 * 60 * 1000 * 30)
const endDate = new Date()
const dataText = ‘’//表格置空
export default {
startDate,
endDate,
dataText,
}


全局使用,示例如下:



import globalVariable from ‘./assets/js/global_variable’
Vue.prototype.GLOBAL = globalVariable


在需要使用的模块文件中使用(无需引入,直接通过`this`使用),示例如下:



data() {
return {
startDate: this.GLOBAL.startDate, //开始时间
endDate: this.GLOBAL.endDate, //结束时间
dataText: this.GLOBAL.dataText, //表格开始置空
};
},


注⚠️:务必以`$`开头,否则会命名冲突!


### 三、全局挂载全局函数


有很多函数在重复使用,所以,我们可以对这些函数进行全局挂载,省时又省力!


实现过程如下:



> 
> 1. 单独新建一个全局变量模块文件,模块中定义一些变量初始状态,用`export default` 暴露出去;
> 2. 在`main.js`中引入&
  • 20
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值