• vue-router 通过路由来实现切换头部标题


    在做单页面应用程序时,一般页面布局头尾两块都是固定在布局页面,中间为是路由入口。这时访问页面时头部标题不会变,该问题的解决方案如下:

    通过采用组件内路由卫士(beforeRouterEnter、beforeRouterUpdate)与路由元信息(meta) 来实现更新头部标题信息。点击查看文档

    beforeRouterEnter:第一次进入时调用。

    beforeRouterUpdate:重复使用当前组件时调用。

    效果图如下:

    注意看页面标题图标变换

     路由元信息(meta)配置

    在路由元信息中配置页面标题,通过组件内路由卫士获取

    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')
            }
        ]
    })

    路由布局页面

     header 与 footer 是固定头尾, main为路由入口。 title为页面标题

    <template>
        <div id="app">
            <header class="header">
                <button @click="back" class="t-xiaoxi iconfont" v-html="icon"></button>
                <h1 class="t-title">{{title}}</h1>
                <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>

    beforeRouteEnterbeforeRouteUpdate函数中获取路由元信息,并更新页面标题。
    beforeRouteEnter:当第一次进入时,会被标题进行一次初始化操作
    beforeRouteUpdate
    :当组件被重复调用时,执行更新操作。
    <script>
        export default {
            name: "app",
            data() {
                return {
                    title: "我的网站",
                    url: '/',
                    icon: '&#xe627;'
                }
            },
            methods: {
                back() {
                    this.$router.go(this.url);
                },
                update(route) {
                    [this.title, this.url, this.icon] = ["我的网站", '/', '&#xe627;'];
                    if (!['', '/'].includes(route.path)) { // 判断是否根页面,用于切换标题与返回上一页或回到主页
                        [this.title, this.url, this.icon] = [route.meta.title || "", '-1', '&#xe81e;'];
                    }
                }
            },
            beforeRouteEnter(to, from, next) {
                next(vm => {  //回调函数,此时this指针不可用,可采用回调函数访问。
                    vm.update(to);
                })
            },
            beforeRouteUpdate(to, from, next) {
                this.update(to);
                next();
            }
        };
    </script>
  • 相关阅读:
    Jmeter之参数化(4种设置方法)
    Pytest+allure安装和框架搭建
    Xshell~工作中访问Linux服务器
    Jmeter之梯度式加压(Stepping Thread Group)
    Jmeter之压测探索和结果分析
    Jmeter之Linux安装(Xshell),分布式运行Linux作为slave机
    Jmeter之Plugin插件,服务器监控
    Jmeter之分布式测试/压测
    傅立叶
    触发器
  • 原文地址:https://www.cnblogs.com/whnba/p/10760896.html
Copyright © 2020-2023  润新知