index.vue 1.9 KB

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