get-current-pages.uvue 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. <template>
  2. <!-- #ifdef APP -->
  3. <scroll-view class="page-scroll-view">
  4. <!-- #endif -->
  5. <view>
  6. <page-head title="getCurrentPages"></page-head>
  7. <view class="uni-padding-wrap">
  8. <button @click="_getCurrentPages">getCurrentPages</button>
  9. <view v-if="pages.length" style="padding: 15px 0px">
  10. <text>当前页面栈中 {{ pages.length }} 个页面,列表如下:</text>
  11. <template v-for="(page, index) in pages" :key="page.route">
  12. <text style="margin-top: 5px">index: {{ index }}, route: {{ page.route }}</text>
  13. </template>
  14. </view>
  15. <!-- #ifndef MP -->
  16. <button class="uni-common-mt" @click="check$page">check $page</button>
  17. <button class="uni-common-mt" @click="checkGetParentPage">
  18. check getParentPage
  19. </button>
  20. <button class="uni-common-mt" @click="checkGetDialogPages">
  21. check getDialogPages
  22. </button>
  23. <button id="check-get-element-by-id-btn" class="uni-common-mt" @click="checkGetElementById">
  24. check getElementById
  25. </button>
  26. <button class="uni-common-mt" @click="checkGetAndroidView">
  27. check getAndroidView
  28. </button>
  29. <button class="uni-common-mt" @click="checkGetIOSView">
  30. check getIOSView
  31. </button>
  32. <button class="uni-common-mt" @click="checkGetHTMLElement">
  33. check getHTMLElement
  34. </button>
  35. <!-- #endif -->
  36. <!-- #ifdef APP-ANDROID -->
  37. <button class="uni-common-mt" @click="checkGetAndroidActivity">
  38. check getAndroidActivity
  39. </button>
  40. <!-- #endif -->
  41. </view>
  42. <!-- #ifndef MP -->
  43. <page-head title="currentPageStyle"></page-head>
  44. <template v-for="(item, index) in PageStyleArray">
  45. <view class="page-style-item" v-if="currentPageStyle[item.key] != null" :key="index">
  46. <view class="item-text">
  47. <text class="item-text-key">{{ item.key }}:</text>
  48. <text class="item-text-value">{{
  49. currentPageStyle[item.key]
  50. }}</text>
  51. </view>
  52. <view class="set-value" v-if="item.type == 'boolean'">
  53. <switch :checked="currentPageStyle.getBoolean(item.key)"
  54. @change="switchChange(item.key, $event as UniSwitchChangeEvent)">
  55. </switch>
  56. </view>
  57. <view class="set-value" v-else-if="item.type == 'number'">
  58. <slider :value="currentPageStyle.getNumber(item.key)" :show-value="true"
  59. @change="sliderChange(item.key, $event as UniSliderChangeEvent)" />
  60. </view>
  61. <view class="set-value" v-else-if="item.type == 'string'">
  62. <radio-group class="radio-set-value" @change="radioChange(item.key, $event as RadioGroupChangeEvent)">
  63. <radio class="radio-value" v-for="(item2, index2) in item.value" :key="index2" :value="item2">{{ item2 }}
  64. </radio>
  65. </radio-group>
  66. </view>
  67. </view>
  68. </template>
  69. <!-- #ifndef APP-HARMONY -->
  70. <button style="margin: 10px" @click="goSetDisablePullDownRefresh">
  71. go set disable pullDownRefresh
  72. </button>
  73. <!-- #endif -->
  74. <!-- #endif -->
  75. </view>
  76. <!-- #ifdef APP -->
  77. </scroll-view>
  78. <!-- #endif -->
  79. </template>
  80. <script>
  81. import { PageStyleItem, PageStyleArray } from './page-style.uts';
  82. class Page {
  83. constructor(public route : string) {
  84. }
  85. }
  86. export default {
  87. data() {
  88. return {
  89. checked: false,
  90. pages: [] as Page[],
  91. PageStyleArray: PageStyleArray as PageStyleItem[],
  92. currentPageStyle: {} as UTSJSONObject,
  93. testing: false
  94. }
  95. },
  96. computed: {
  97. pageStyleText() : string {
  98. return JSON.stringify(this.currentPageStyle)
  99. }
  100. },
  101. // #ifndef MP
  102. onLoad() {
  103. this.getPageStyle();
  104. },
  105. // #endif
  106. onReady() {
  107. // #ifdef APP-ANDROID
  108. this.setPageStyle({
  109. 'androidThreeButtonNavigationBackgroundColor': 'transparent',
  110. 'androidThreeButtonNavigationStyle': 'black'
  111. });
  112. this.getPageStyle();
  113. // #endif
  114. },
  115. onPullDownRefresh() {
  116. setTimeout(() => {
  117. uni.stopPullDownRefresh()
  118. }, 2000)
  119. },
  120. methods: {
  121. startPullDownRefresh() {
  122. uni.startPullDownRefresh()
  123. },
  124. _getCurrentPages: function () {
  125. this.pages.length = 0
  126. const pages = getCurrentPages()
  127. this.pages.push(new Page(pages[0].route))
  128. if (this.pages[0].route.includes('/tabBar/') || this.pages[0].route == '/') {
  129. this.checked = true
  130. }
  131. for (let i = 1; i < pages.length; i++) {
  132. this.pages.push(new Page(pages[i].route))
  133. if (pages[i].route.includes('/tabBar/')) {
  134. this.checked = false
  135. }
  136. }
  137. },
  138. /// get-set-page-style
  139. radioChange(key : string, e : RadioGroupChangeEvent) {
  140. this.setStyleValue(key, e.detail.value);
  141. },
  142. sliderChange(key : string, e : UniSliderChangeEvent) {
  143. this.setStyleValue(key, e.detail.value);
  144. },
  145. switchChange(key : string, e : UniSwitchChangeEvent) {
  146. this.setStyleValue(key, e.detail.value);
  147. },
  148. setStyleValue(key : string, value : any) {
  149. const style = {}
  150. style[key] = value
  151. this.setPageStyle(style)
  152. this.getPageStyle()
  153. },
  154. getPageStyle() : UTSJSONObject {
  155. const pages = getCurrentPages();
  156. const currentPage = pages[pages.length - 1];
  157. this.currentPageStyle = currentPage.getPageStyle()
  158. console.log(this.currentPageStyle)
  159. return this.currentPageStyle;
  160. },
  161. setPageStyle(style : UTSJSONObject) {
  162. console.log('setPageStyle:', style);
  163. const pages = getCurrentPages();
  164. const currentPage = pages[pages.length - 1];
  165. currentPage.setPageStyle(style);
  166. },
  167. goSetDisablePullDownRefresh() {
  168. uni.navigateTo({
  169. url: '/pages/API/get-current-pages/set-page-style-disable-pull-down-refresh'
  170. });
  171. },
  172. getCurrentPage() : UniPage {
  173. const pages = getCurrentPages()
  174. return pages[pages.length - 1]
  175. },
  176. check$page() : boolean {
  177. const page = this.getCurrentPage()
  178. let res = this.$page === page
  179. if (this.testing && res) {
  180. res = page.options['test'] == '123'
  181. if (res) {
  182. res = page.route == 'pages/API/get-current-pages/get-current-pages'
  183. }
  184. }
  185. console.log('check $page', res)
  186. uni.showToast(res ? { title: 'check success' } : { title: 'check fail', icon: 'error' })
  187. return res
  188. },
  189. checkGetParentPage() : boolean {
  190. const page = this.getCurrentPage()
  191. const parentPage = page.getParentPage()
  192. const res = parentPage == null
  193. console.log('check getParentPage', res)
  194. uni.showToast(res ? { title: 'check success' } : { title: 'check fail', icon: 'error' })
  195. return res
  196. },
  197. checkGetDialogPages() : boolean {
  198. const page = this.getCurrentPage()
  199. const dialogPages = page.getDialogPages()
  200. const res = Array.isArray(dialogPages) && dialogPages.length == 0
  201. uni.showToast(res ? { title: 'check success' } : { title: 'check fail', icon: 'error' })
  202. console.log('check getDialogPages', res)
  203. return res
  204. },
  205. checkGetElementById() : boolean {
  206. const page = this.getCurrentPage()
  207. const element = page.getElementById('check-get-element-by-id-btn')
  208. let res = element != null
  209. // #ifndef APP-ANDROID
  210. if (res) {
  211. const elPage = element!.getPage()
  212. console.log('elPage', elPage)
  213. res = elPage === page
  214. }
  215. // #endif
  216. console.log('check getElementById', res)
  217. uni.showToast(res ? { title: 'check success' } : { title: 'check fail', icon: 'error' })
  218. return res
  219. },
  220. checkGetAndroidView() : boolean {
  221. const page = this.getCurrentPage()
  222. const androidView = page.getAndroidView()
  223. const res = androidView != null
  224. console.log('check getAndroidView', res)
  225. uni.showToast(res ? { title: 'check success' } : { title: 'check fail', icon: 'error' })
  226. return res
  227. },
  228. checkGetIOSView() : boolean {
  229. const page = this.getCurrentPage()
  230. const IOSView = page.getIOSView()
  231. const res = IOSView != null
  232. console.log('check getIOSView', res)
  233. uni.showToast(res ? { title: 'check success' } : { title: '仅 IOS uts 插件环境支持', icon: 'error' })
  234. return res
  235. },
  236. checkGetHTMLElement() : boolean {
  237. const page = this.getCurrentPage()
  238. const HTMLView = page.getHTMLElement()
  239. const res = HTMLView != null
  240. console.log('check getHTMLElement', res)
  241. uni.showToast(res ? { title: 'check success' } : { title: 'check fail', icon: 'error' })
  242. return res
  243. },
  244. checkGetAndroidActivity() : boolean {
  245. const page = this.getCurrentPage()
  246. const activity = page.getAndroidActivity()
  247. const res = activity != null
  248. console.log('check getAndroidActivity', res)
  249. uni.showToast(res ? { title: 'check success' } : { title: 'check getAndroidActivity', icon: 'error' })
  250. return res
  251. }
  252. },
  253. }
  254. </script>
  255. <style>
  256. .page {
  257. flex: 1;
  258. padding: 10px;
  259. }
  260. .page-style {
  261. margin-top: 15px;
  262. }
  263. .page-style-item {
  264. padding: 10px;
  265. margin-top: 10px;
  266. background-color: #ffffff;
  267. border-radius: 5px;
  268. }
  269. .item-text {
  270. flex-direction: row;
  271. }
  272. .item-text-key {
  273. font-weight: bold;
  274. }
  275. .item-text-value {
  276. margin-left: 5px;
  277. }
  278. .set-value {
  279. margin-top: 10px;
  280. }
  281. .radio-set-value {
  282. flex-direction: row;
  283. }
  284. .radio-value {
  285. margin-left: 10px;
  286. }
  287. </style>