get-current-pages.uvue 5.3 KB
Newer Older
1 2 3 4
<template>
  <!-- #ifdef APP -->
  <scroll-view class="page-scroll-view">
  <!-- #endif -->
5 6 7 8 9 10 11 12 13 14
    <view>
      <page-head title="getCurrentPages"></page-head>
      <view class="uni-padding-wrap">
        <button @click="_getCurrentPages">getCurrentPages</button>
        <view v-if="pages.length" style="padding: 15px 0px">
          <text>当前页面栈中 {{ pages.length }} 个页面,列表如下:</text>
          <template v-for="(page, index) in pages" :key="page.route">
            <text style="margin-top: 5px">index: {{ index }}, route: {{ page.route }}</text>
          </template>
        </view>
15 16
      </view>

17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
      <page-head title="currentPageStyle"></page-head>
      <template v-for="(item, index) in PageStyleArray">
        <view class="page-style-item" v-if="currentPageStyle[item.key]!=null" :key="index">
          <view class="item-text">
            <text class="item-text-key">{{item.key}}:</text>
            <text class="item-text-value">{{currentPageStyle[item.key]}}</text>
          </view>
          <view class="set-value" v-if="item.type == 'boolean'">
            <switch :checked="currentPageStyle.getBoolean(item.key)"
              @change="switchChange(item.key, $event as UniSwitchChangeEvent)">
            </switch>
          </view>
          <view class="set-value" v-else-if="item.type == 'number'">
            <slider :value="currentPageStyle.getNumber(item.key)" :show-value="true"
              @change="sliderChange(item.key, $event as UniSliderChangeEvent)" />
          </view>
          <view class="set-value" v-else-if="item.type == 'string'">
            <radio-group class="radio-set-value" @change="radioChange(item.key, $event as RadioGroupChangeEvent)">
              <radio class="radio-value" v-for="(item2, index2) in item.value" :key="index2" :value="item2">{{item2}}
              </radio>
            </radio-group>
          </view>
39
        </view>
40 41 42
      </template>
      <button style='margin: 10px;' @click="goSetDisablePullDownRefresh">go set disable pullDownRefresh</button>
    </view>
43 44
  <!-- #ifdef APP -->
  </scroll-view>
H
hdx 已提交
45
  <!-- #endif -->
46 47
</template>

48 49
<script>
  import { PageStyleItem, PageStyleArray } from './page-style.uts';
H
hdx 已提交
50

51 52 53 54 55 56 57 58 59
  class Page {
    constructor(public route : string) {
    }
  }

  export default {
    data() {
      return {
        checked: false,
60 61
        pages: [] as Page[],
        PageStyleArray: PageStyleArray as PageStyleItem[],
62
        currentPageStyle: {} as UTSJSONObject,
63 64 65 66 67 68
      }
    },
    computed: {
      pageStyleText() : string {
        return JSON.stringify(this.currentPageStyle)
      }
69 70 71
    },
    onLoad() {
      this.getPageStyle();
72 73 74 75
    },
    onPullDownRefresh() {
      setTimeout(() => {
        uni.stopPullDownRefresh()
H
hdx 已提交
76
      }, 2000)
77
    },
78 79 80
    methods: {
      startPullDownRefresh() {
        uni.startPullDownRefresh()
H
hdx 已提交
81
      },
82 83 84 85 86 87 88 89 90 91 92 93 94
      _getCurrentPages: function () {
        this.pages.length = 0
        const pages = getCurrentPages()
        this.pages.push(new Page(pages[0].route))
        if (this.pages[0].route.includes('/tabBar/')) {
          this.checked = true
        }
        for (let i = 1; i < pages.length; i++) {
          this.pages.push(new Page(pages[i].route))
          if (pages[i].route.includes('/tabBar/')) {
            this.checked = false
          }
        }
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
      },
      /// get-set-page-style
      radioChange(key : string, e : RadioGroupChangeEvent) {
        this.setStyleValue(key, e.detail.value);
      },
      sliderChange(key : string, e : UniSliderChangeEvent) {
        this.setStyleValue(key, e.detail.value);
      },
      switchChange(key : string, e : UniSwitchChangeEvent) {
        this.setStyleValue(key, e.detail.value);
      },
      setStyleValue(key : string, value : any) {
        const style = {}
        style[key] = value
        this.setPageStyle(style)
        this.getPageStyle()
      },
      getPageStyle() : UTSJSONObject {
        const pages = getCurrentPages();
        const currentPage = pages[pages.length - 1];
115
        this.currentPageStyle = currentPage.getPageStyle()
116 117 118 119 120 121
        return this.currentPageStyle;
      },
      setPageStyle(style : UTSJSONObject) {
        console.log('setPageStyle:', style);
        const pages = getCurrentPages();
        const currentPage = pages[pages.length - 1];
122
        currentPage.setPageStyle(style);
H
hdx 已提交
123
      },
124 125 126 127 128
      goSetDisablePullDownRefresh() {
        uni.navigateTo({
          url: '/pages/API/get-current-pages/set-page-style-disable-pull-down-refresh'
        });
      }
129 130 131 132 133 134 135 136 137 138
      // getCurrentPage(): Page {
      //   const pages = getCurrentPages();
      //   const currentPage = pages[pages.length - 1];
      //   return currentPage;
      // }
    },
  }
</script>

<style>
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
  .page {
    flex: 1;
    padding: 10px;
  }

  .page-style {
    margin-top: 15px;
  }

  .page-style-item {
    padding: 10px;
    margin-top: 10px;
    background-color: #ffffff;
    border-radius: 5px;
  }

  .item-text {
    flex-direction: row;
  }

  .item-text-key {
    font-weight: bold;
  }

  .item-text-value {
164
    margin-left: 5px;
165 166 167 168
  }

  .set-value {
    margin-top: 10px;
169
  }
170 171 172 173 174

  .radio-set-value {
    flex-direction: row;
  }

175 176
  .radio-value {
    margin-left: 10px;
177
  }
178
</style>