set-navigation-bar-title.uvue 1.8 KB
Newer Older
DCloud-WZF's avatar
DCloud-WZF 已提交
1 2 3 4
<template>
  <view>
    <page-head title="setNavigationBarTitle"></page-head>
    <view class="uni-padding-wrap uni-common-mt">
5 6 7 8 9
      <button @tap="setNavigationBarNewTitle" class="uni-btn">
        设置当前页面标题为: {{ newTitle }}
      </button>
      <button @tap="setNavigationBarLongTitle" class="uni-btn">
        设置超长标题
DCloud-WZF's avatar
DCloud-WZF 已提交
10 11 12 13 14 15 16 17 18 19 20
      </button>
    </view>
  </view>
</template>

<script lang="uts">
import { state, setLifeCycleNum } from '@/store/index.uts'

export default {
  data(){
    return {
21 22
      newTitle: 'new title',
      longTitle: 'long title long title long title long title long title long title long title long title long title long title'
DCloud-WZF's avatar
DCloud-WZF 已提交
23 24 25
    }
  },
  methods: {
26
    setNavigationBarNewTitle() {
DCloud-WZF's avatar
DCloud-WZF 已提交
27 28
      uni.setNavigationBarTitle({
        title: this.newTitle,
29
        success: () => {
DCloud-WZF's avatar
DCloud-WZF 已提交
30 31 32
          console.log('setNavigationBarTitle success')
          this.setLifeCycleNum(state.lifeCycleNum + 1)
        },
33
        fail: () => {
34
          console.log('setNavigationBarTitle fail')
DCloud-WZF's avatar
DCloud-WZF 已提交
35 36
          this.setLifeCycleNum(state.lifeCycleNum - 1)
        },
37
        complete: () => {
DCloud-WZF's avatar
DCloud-WZF 已提交
38 39 40 41 42
          console.log('setNavigationBarTitle complete')
          this.setLifeCycleNum(state.lifeCycleNum + 1)
        }
      })
    },
43 44 45 46 47 48 49 50 51 52 53 54 55 56
    setNavigationBarLongTitle() {
      uni.setNavigationBarTitle({
        title: this.longTitle,
        success() {
          console.log('setNavigationBarTitle success')
        },
        fail() {
          console.log('setNavigationBarTitle fail')
        },
        complete() {
          console.log('setNavigationBarTitle complete')
        }
      })
    },
DCloud-WZF's avatar
DCloud-WZF 已提交
57 58 59 60 61 62 63 64 65 66 67
    // 自动化测试
    getLifeCycleNum(): number {
      return state.lifeCycleNum
    },
    // 自动化测试
    setLifeCycleNum(num: number) {
      setLifeCycleNum(num)
    }
  },
}
</script>