当前位置:网站首页>Uni app implements global variables

Uni app implements global variables

2022-07-05 09:07:00 Hang Hang Dad

4、globalData

Applet can be used in App Statement above Global variables , But in Vue There is no ,uni-app In the App.vue Can be defined in globalData Attribute , Can also be used API Read and write this value .

This way supports vue and nvue Shared data . Is currently the nvue and vue A better way to share data .

1, Definition :App.vue Define methods or variables

<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>  
  /* Every page is public css */  
</style>

2, Operation in page globalData The way

assignment :

getApp().globalData.text = 'test'

Value :

console.log(getApp().globalData.text)

Use cases :

import app from "../../App.vue"    /// This sentence is more important   You can try Delete and add this sentence
// there vm, Is that we are vue In the document this, So we can get it here vuex The variable of , Like what's in it token Variable
const install = (Vue, vm) => {
    // This is a custom configuration parameter , See the above description for specific parameters
    Vue.prototype.$u.http.setConfig({
        baseUrl: app.globalData.apiUrl,
        loadingText: 'loading...',
        loadingTime: 300,
        originalData: false, // Whether to return the original data of the server in the interceptor
        loadingMask: true, // Exhibition loading When , Whether to give a transparent mask , Prevent touch penetration
        
        // ......
    });
 

原网站

版权声明
本文为[Hang Hang Dad]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/186/202207050846043153.html