当前位置:网站首页>uni-app 实现全局变量
uni-app 实现全局变量
2022-07-05 08:46:00 【杭杭爸爸】
4、globalData
小程序中能够在 App 上声明全局变量,但在 Vue 中没有,uni-app 中在 App.vue 能够定义在 globalData 属性上,也能够使用 API 读写这个值。
这个方式支持vue和nvue共享数据。是目前nvue和vue共享数据的一种比较好的方式。
1,定义:App.vue 定义方法或变量
<script> export default { globalData: { text: 'text' }, onLaunch: function() { console.log('App Launch') }, onShow: function() { console.log('App Show') }, onHide: function() { console.log('App Hide') } } </script> <style> /*每一个页面公共css */ </style>
2,页面中操做 globalData 的方式
赋值:
getApp().globalData.text = 'test'
取值:
console.log(getApp().globalData.text)
使用案例:
import app from "../../App.vue" ///这句比较重要 可以尝试 删除添加这句话
// 这里的vm,就是我们在vue文件里面的this,所以我们能在这里获取vuex的变量,比如存放在里面的token变量
const install = (Vue, vm) => {
// 此为自定义配置参数,具体参数见上方说明
Vue.prototype.$u.http.setConfig({
baseUrl: app.globalData.apiUrl,
loadingText: 'loading...',
loadingTime: 300,
originalData: false, // 是否在拦截器中返回服务端的原始数据
loadingMask: true, // 展示loading的时候,是否给一个透明的蒙层,防止触摸穿透
// ......
});
边栏推荐
- File server migration scheme of a company
- 猜谜语啦(7)
- Array, date, string object method
- Run菜单解析
- [daiy4] jz32 print binary tree from top to bottom
- Chris LATTNER, the father of llvm: why should we rebuild AI infrastructure software
- Wechat H5 official account to get openid climbing account
- 猜谜语啦(9)
- Count of C # LINQ source code analysis
- Guess riddles (4)
猜你喜欢
EA introduction notes
The combination of deep learning model and wet experiment is expected to be used for metabolic flux analysis
Business modeling of software model | object modeling
[牛客网刷题 Day4] JZ35 复杂链表的复制
C [essential skills] use of configurationmanager class (use of file app.config)
Guess riddles (3)
Halcon Chinese character recognition
Classification of plastic surgery: short in long long long
Halcon snap, get the area and position of coins
C#【必备技能篇】ConfigurationManager 类的使用(文件App.config的使用)
随机推荐
Halcon blob analysis (ball.hdev)
Basic number theory - fast power
[matlab] matlab reads and writes Excel
Add discount recharge and discount shadow ticket plug-ins to the resource realization applet
Task failed task_ 1641530057069_ 0002_ m_ 000000
Guess riddles (11)
猜谜语啦(7)
暑假第一周
The combination of deep learning model and wet experiment is expected to be used for metabolic flux analysis
Meta tag details
IT冷知识(更新ing~)
My university
Halcon Chinese character recognition
kubeadm系列-00-overview
Basic number theory - factors
Numpy pit: after the addition of dimension (n, 1) and dimension (n,) array, the dimension becomes (n, n)
【日常训练】1200. 最小绝对差
猜谜语啦(8)
Halcon color recognition_ fuses. hdev:classify fuses by color
asp.net(c#)的货币格式化