zhangxiaoxu123456
2021-12-06 f32f7d699607bb159d25dcf441942370aba2c968
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
<template>
    <view class="u-demo">
        <view class="u-demo-wrap">
            <view class="u-demo-title">演示效果</view>
            <view class="u-demo-area">
                <view class="u-no-demo-here">
                    globalData方案的值为(曲折实现,全局动态响应)
                </view>
                <view class="u-demo-result-line">
                    {{globalData}}
                </view>
            </view>
            <view class="u-demo-area">
                <view class="u-no-demo-here">
                    Vue.prototype方案的值为(非动态响应,微信小程序无效)
                </view>
                <view class="u-demo-result-line">
                    {{vuePrototype}}
                </view>
            </view>
            <view class="u-demo-area">
                <view class="u-no-demo-here">
                    vuex方案的值为(全局动态响应,推荐)
                </view>
                <view class="u-demo-result-line">
                    {{vuex_demo}}
                </view>
            </view>
        </view>
        <view class="u-config-wrap">
            <view class="u-config-title u-border-bottom">
                参数配置
            </view>
            <view class="u-config-item">
                <view class="u-item-title">实现方式</view>
                <view class="btn-wrap">
                    <u-button @click="modeChange(0)">globalData</u-button>
                </view>
                <view class="btn-wrap">
                    <u-button @click="modeChange(1)">Vue.prototype</u-button>
                </view>
                <view class="btn-wrap">
                    <u-button @click="modeChange(2)">vuex</u-button>
                </view>
            </view>
        </view>
    </view>
</template>
 
<script>
    export default {
        data() {
            return {
                globalData: ''
            }
        },
        onShow() {
            // 对globalData的使用,应在onShow生命周期,而不是onLoad生命周期
            this.globalData = getApp().globalData.username;
        },
        methods: {
            modeChange(index) {
                let url = '';
                if(index == 0) url = '/pages/library/globalVariable/globalData'; 
                if(index == 1) url = '/pages/library/globalVariable/prototype';
                if(index == 2) url = '/pages/library/globalVariable/vuex';
                this.$u.route(url);
            },
        }
    }
</script>
 
<style lang="scss" scoped>
    .u-demo-area {
        margin-top: 50rpx;
    }
    
    .btn-wrap {
        margin-top: 40rpx;
        padding: 0 10%;
    }
</style>