globalData.vue 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. <template>
  2. <view class="u-demo">
  3. <view class="u-demo-wrap">
  4. <view class="u-demo-title">演示效果</view>
  5. <view class="u-demo-area">
  6. <view class="u-no-demo-here">
  7. 此处为演示globalData全局变量的使用,需手动更新
  8. </view>
  9. <view class="u-demo-result-line">
  10. {{result}}
  11. </view>
  12. </view>
  13. </view>
  14. <view class="u-config-wrap">
  15. <view class="u-config-title u-border-bottom">
  16. 参数配置
  17. </view>
  18. <view class="u-config-item">
  19. <view class="u-item-title">修改globalData为如下值</view>
  20. <u-subsection :list="['雅蓝', '皓白', '橘黄']" @change="globalDataChange"></u-subsection>
  21. </view>
  22. </view>
  23. </view>
  24. </template>
  25. <script>
  26. export default {
  27. data() {
  28. return {
  29. result: null
  30. }
  31. },
  32. onShow() {
  33. // 对globalData的使用,应在onShow生命周期,而不是onLoad生命周期
  34. this.result = getApp().globalData.username;
  35. },
  36. methods: {
  37. globalDataChange(index) {
  38. getApp().globalData.username = index == 0 ? '雅蓝' : index == 1 ? '皓白' : '橘黄';
  39. // 在此改变了globalData,想要再次触发本页内容更细,还必须手动操作
  40. this.result = getApp().globalData.username;
  41. }
  42. }
  43. }
  44. </script>
  45. <style lang="scss" scoped>
  46. .u-demo {}
  47. </style>