当前位置:网站首页>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的时候,是否给一个透明的蒙层,防止触摸穿透
// ......
});
边栏推荐
- [daiy4] copy of JZ35 complex linked list
- golang 基础 ——map、数组、切片 存放不同类型的数据
- Redis implements a high-performance full-text search engine -- redisearch
- Confusing basic concepts member variables local variables global variables
- Basic number theory -- Euler function
- Halcon shape_ trans
- Business modeling of software model | object modeling
- Kubedm series-00-overview
- [牛客网刷题 Day4] JZ32 从上往下打印二叉树
- c#比较两张图像的差异
猜你喜欢

Confusing basic concepts member variables local variables global variables

Classification of plastic surgery: short in long long long

Guess riddles (10)

How apaas is applied in different organizational structures

Halcon Chinese character recognition

猜谜语啦(8)

Guess riddles (9)

EA introduction notes

Business modeling of software model | object modeling

Programming implementation of ROS learning 2 publisher node
随机推荐
猜谜语啦(10)
Basic number theory - factors
Business modeling | process of software model
Tips 1: Web video playback code
ABC#237 C
Infix expression evaluation
Business modeling of software model | overview
猜谜语啦(8)
ORACLE进阶(三)数据字典详解
Programming implementation of ROS learning 6 -service node
Several problems to be considered and solved in the design of multi tenant architecture
Confusing basic concepts member variables local variables global variables
How apaas is applied in different organizational structures
The first week of summer vacation
One dimensional vector transpose point multiplication np dot
How many checks does kubedm series-01-preflight have
c#比较两张图像的差异
Run菜单解析
C#绘制带控制点的Bezier曲线,用于点阵图像及矢量图形
Meta tag details