uni-app: setting and reference of global variables (getApp().globalData)

 style:

First, find the App.vue file in the root directory


Write global variables in App.vue

<script>
	export default {
		onLaunch: function() {
			console.log('App Launch')
		},
		onShow: function() {
			console.log('App Show')
		},
		onHide: function() {
			console.log('App Hide')
		},
		globalData:{
			test1:'这是一个测试'
		}
	}
</script>

<style>
	/*每个页面公共css */
</style>

 globalData: {             test1: 'This is a test'         }

 Set data on the foreground page and display it to the front end

<template>
	<view class="content">
		<text class="title">{
   
   {test1}}</text>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				test1: getApp().globalData.test1
			}
		},
		onLoad() {
			console.log(getApp().globalData.test1)			
		},
		methods: {

		}
	}
</script>

<style>
	
</style>

data setting

data() {
            return {
                test1: getApp().globalData.test1
            }
        },

 display to the front

<view class="content">
        <text class="title">{ {test1}}</text>
    </view>

Guess you like

Origin blog.csdn.net/weixin_46001736/article/details/131822977