溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶(hù)服務(wù)條款》

vue router 通過(guò)路由來(lái)實(shí)現(xiàn)切換頭部標(biāo)題功能

發(fā)布時(shí)間:2020-09-25 19:56:31 來(lái)源:腳本之家 閱讀:229 作者:破殼而出的蝌蚪 欄目:web開(kāi)發(fā)

在做單頁(yè)面應(yīng)用程序時(shí),一般頁(yè)面布局頭尾兩塊都是固定在布局頁(yè)面,中間為是路由入口。這時(shí)訪(fǎng)問(wèn)頁(yè)面時(shí)頭部標(biāo)題不會(huì)變,該問(wèn)題的解決方案如下:

通過(guò)采用組件內(nèi)路由衛(wèi)士(beforeRouterEnter、beforeRouterUpdate)與路由元信息(meta) 來(lái)實(shí)現(xiàn)更新頭部標(biāo)題信息。點(diǎn)擊查看文檔

beforeRouterEnter:第一次進(jìn)入時(shí)調(diào)用。

beforeRouterUpdate:重復(fù)使用當(dāng)前組件時(shí)調(diào)用。

效果圖如下:

vue router 通過(guò)路由來(lái)實(shí)現(xiàn)切換頭部標(biāo)題功能

注意看頁(yè)面標(biāo)題與圖標(biāo)變換

 路由元信息(meta)配置

在路由元信息中配置頁(yè)面標(biāo)題,通過(guò)組件內(nèi)路由衛(wèi)士獲取

const router = new Router({
  mode: 'history',
  base: process.env.BASE_URL,
  routes: [
    {
      path: "help",
      name: "help",
      meta: {
        title: "新手幫助"
      },
      component: () => import('./views/Help.vue')
    },
    {
      path: "page",
      name: "page",
      meta: {
        title: "寶貝信息"
      },
      component: () => import('./views/Page.vue')
    }
  ]
})

路由布局頁(yè)面

 header 與 footer 是固定頭尾, main為路由入口。 title為頁(yè)面標(biāo)題

<template>
  <div id="app">
    <header class="header">
      <button @click="back" class="t-xiaoxi iconfont" v-html="icon"></button>
      <h2 class="t-title">{{title}}</h2>
      <router-link to="/search" class="t-sousuo iconfont">&#xe611;</router-link>
    </header>
    <div class="main">
      <router-view></router-view>
    </div>
    <footer class="footer">
      // ...
    </footer>
  </div>
</template>

在beforeRouteEnter、beforeRouteUpdate函數(shù)中獲取路由元信息,并更新頁(yè)面標(biāo)題。

beforeRouteEnter:當(dāng)?shù)谝淮芜M(jìn)入時(shí),會(huì)被標(biāo)題進(jìn)行一次初始化操作

beforeRouteUpdate:當(dāng)組件被重復(fù)調(diào)用時(shí),執(zhí)行更新操作。

<script>
  export default {
    name: "app",
    data() {
      return {
        title: "我的網(wǎng)站",
        url: '/',
        icon: '&#xe627;'
      }
    },
    methods: {
      back() {
        this.$router.go(this.url);
      },
      update(route) {
        [this.title, this.url, this.icon] = ["我的網(wǎng)站", '/', '&#xe627;'];
        if (!['', '/'].includes(route.path)) { // 判斷是否根頁(yè)面,用于切換標(biāo)題與返回上一頁(yè)或回到主頁(yè)
          [this.title, this.url, this.icon] = [route.meta.title || "", '-1', '&#xe81e;'];
        }
      }
    },
    beforeRouteEnter(to, from, next) {
      next(vm => { //回調(diào)函數(shù),此時(shí)this指針不可用,可采用回調(diào)函數(shù)訪(fǎng)問(wèn)。
        vm.update(to);
      })
    },
    beforeRouteUpdate(to, from, next) {
      this.update(to);
      next();
    }
  };
</script>

總結(jié)

以上所述是小編給大家介紹的vue router 通過(guò)路由來(lái)實(shí)現(xiàn)切換頭部標(biāo)題功能,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)億速云網(wǎng)站的支持!
如果你覺(jué)得本文對(duì)你有幫助,歡迎轉(zhuǎn)載,煩請(qǐng)注明出處,謝謝!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI