get-current-pages.uvue 1.5 KB
Newer Older
DCloud-WZF's avatar
DCloud-WZF 已提交
1
<template>
DCloud-WZF's avatar
DCloud-WZF 已提交
2 3
  <view>
    <page-head title="getCurrentPages"></page-head>
4 5 6
    <view class="uni-padding-wrap">
      <button @click="_getCurrentPages">getCurrentPages</button>
      <button class="uni-common-mt" @click="hideAndShow">页面隐藏并显示</button>
DCloud-WZF's avatar
DCloud-WZF 已提交
7
      <view v-if="pages.length" style="padding: 15px 0px">
8 9 10 11 12 13 14
        <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>
DCloud-WZF's avatar
DCloud-WZF 已提交
15 16
    </view>
  </view>
DCloud-WZF's avatar
DCloud-WZF 已提交
17
</template>
DCloud-WZF's avatar
DCloud-WZF 已提交
18

DCloud-WZF's avatar
DCloud-WZF 已提交
19
<script lang="uts">
DCloud-WZF's avatar
DCloud-WZF 已提交
20 21 22 23
class Page {
	constructor(public route : string) {
	}
}
DCloud-WZF's avatar
DCloud-WZF 已提交
24

DCloud-WZF's avatar
DCloud-WZF 已提交
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
export default {
	data() {
		return {
			checked: false,
			showTimes: 0,
			pages: [] as Page[],
		}
	},
	onShow() {
		this.showTimes++
	},
	methods: {
		_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
43
			}
DCloud-WZF's avatar
DCloud-WZF 已提交
44 45 46 47
			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
48
				}
DCloud-WZF's avatar
DCloud-WZF 已提交
49
			}
50
		},
DCloud-WZF's avatar
DCloud-WZF 已提交
51 52 53 54 55 56 57 58 59 60 61 62
		hideAndShow() {
			const pages = getCurrentPages()
			const currentPage = pages[pages.length - 1]
			const currentPageInstance = currentPage.$getAppPage()
			if (currentPageInstance != null) {
				currentPageInstance.hide(new Map())
				currentPageInstance.show(new Map())
			}
		}
	},
}
</script>