Vue开发精要之底部导航栏

曾经终败给现在 2021-08-30 23:29 432阅读 0赞

Vue开发精要之底部导航栏

在我们日常开发中,尤其是移动端开发(H5微信公众号企业微信等)中,我们往往需要自己设计底部导航栏。或许,导航栏这种常见功能,大家都是司空见惯的,但是其中设计的知识点却是不少,本片文档将要介绍地步导航栏涉及的知识点与常用方法,共同学习,共同进步。

一、导航切换

  • HTML

    1. <!-- 占位容器 -->
    2. <div class="placegolder-container"></div>
    3. <!-- 底部导航栏 -->
    4. <div class="bottom-tabs">
    5. <div class="tabs-item" v-for="(item, index) in tabsList" :key="index" @click="tabsChange(index)">
    6. <img class="tab-icon" :src="tabIndex==index?item.srcS:item.src">
    7. <p class="tab-text" :class="tabIndex==index?'active':''">{
    8. {item.text}}</p>
    9. </div>
    10. </div>
  • CSS

    1. .placegolder-container {
    2. height: 70px;
    3. }
    4. .bottom-tabs {
    5. position: fixed;
    6. bottom: 0;
    7. left: 0;
    8. right: 0;
    9. z-index: 5;
    10. display: flex;
    11. flex-direction: row;
    12. justify-content: space-around;
    13. align-items: center;
    14. box-shadow: 0px -1px 1px #e6e6e6;
    15. background-color: #fff; .tabs-item {
    16. padding: 5px 0;
    17. flex: 1;
    18. height: 60px;
    19. display: flex;
    20. flex-direction: column;
    21. justify-content: space-around;
    22. align-items: center; .tab-icon {
    23. width: 30px;
    24. height: 30px;
    25. border-radius: 4px;
    26. }
    27. .tab-text {
    28. font-size: 14px; &.active {
    29. color: blue;
    30. }
    31. }
    32. }
    33. }
  • JS

    1. export default {
    2. name: "app",
    3. components: { },
    4. created() {
    5. this.tabIndex = localStorage.getItem("tabIndex");
    6. console.log(this.tabIndex, "222222222222222");
    7. },
    8. data() {
    9. return {
    10. tabIndex: 0,
    11. tabsList: [
    12. {
    13. src: require("../src/assets/images/2.png"),
    14. srcS: require("../src/assets/images/1.png"),
    15. text: "首页",
    16. path: "/booking"
    17. },
    18. {
    19. src: require("../src/assets/images/2.png"),
    20. srcS: require("../src/assets/images/1.png"),
    21. text: "查询",
    22. path: "/query"
    23. },
    24. {
    25. src: require("../src/assets/images/2.png"),
    26. srcS: require("../src/assets/images/1.png"),
    27. text: "信息",
    28. path: "/info"
    29. },
    30. {
    31. src: require("../src/assets/images/2.png"),
    32. srcS: require("../src/assets/images/1.png"),
    33. text: "我的",
    34. path: "/mine"
    35. }
    36. ]
    37. };
    38. },
    39. methods: {
    40. tabsChange(index) {
    41. this.tabIndex = index;
    42. this.$router.push({
    43. path: this.tabsList[index].path
    44. });
    45. console.log(this.tabsList[index].path);
    46. localStorage.setItem("tabIndex", this.tabIndex);
    47. }
    48. },
    49. watch: {
    50. $route(newVal, oldVal) {
    51. console.log(newVal, oldVal);
    52. if (newVal.meta.index) {
    53. this.tabIndex = newVal.meta.index;
    54. localStorage.setItem("tabIndex", this.tabIndex);
    55. }
    56. }
    57. }
    58. };
    59. </script>
  • 注意:

    1. 页面设置占位容器是为了抵消底部导航栏固定定位的高度。
    2. tabIndex 标记当前选中的路由。
    3. tabsChange(index) 底部导航栏路由切换。
    4. watch 监听路由变化,保持路由选中的驻留(选中/激活)样式。

二、缓存使用

  • 为什么要使用缓存?缓存哪些内容?
  • 答:使用缓存是为了保存选中路由的 tabIndex ,在刷新页面的时候,依然可以保持(选中/激活)状态。

三、路由配置与监听

(1)配置
  • router.js

    1. {
    2. path: '/booking',
    3. name: 'booking',
    4. meta: { index: 0, title: '首页' },
    5. component: () => import ('../src/pages/booking/booking.vue')
    6. },
    7. {
    8. path: '/query',
    9. name: 'query',
    10. meta: { index: 1, title: '查询' },
    11. component: () => import ('../src/pages/query/query.vue')
    12. },
  • 在配置路由的时候,我们可以添加meta.titlemeta.index属性,用于与选中路由的 tabIndex
(2)监听
  • 注意:使用监听的目的是为了在使用浏览器前进后退的时候,保持路由选中的驻留(选中/激活)样式。代码见 CSS

四、动态修改页面标题

  • 在修改页面标题 title 的时候,我们配置路由的 title 属性来控制。

    1. // 根据路由设置标题
    2. router.beforeEach((to, from, next) => {
    3. console.log({ to, from })
    4. /*路由发生改变修改页面的title */
    5. if (to.meta.title) {
    6. document.title = to.meta.title
    7. }
    8. next();
    9. })
  • 另外,在涉及详情相关的页面的时候,我们往往需要使用详情中的某一属性来设置标题,这个时候我们可以通过,在页面的 created 中使用方法修改。具体如下:

    1. created() {
    2. document.title = this.currentRoomInfo.name;
    3. },

五、效果图

页面展示


六、感悟

在实际开发中,如果我们作为开发者,很多时候我们应该从用户测试运维的角度多多考虑,这个我们的代码慢慢就会变得完美,至少逻辑上没有什么错误,这样自己才能慢慢进步……

发表评论

表情:
评论列表 (有 0 条评论,432人围观)

还没有评论,来说两句吧...

相关阅读

    相关 底部导航

    1.添加相应的文件 2.分别加入4个Fragment以及布局文件 3.MianActivity的引用 4.MainActivity的布局文件   添加相应的文件:

    相关 Vue开发底部导航

    Vue开发精要之底部导航栏 > 在我们日常开发中,尤其是移动端开发(`H5`、`微信公众号`、`企业微信`等)中,我们往往需要自己设计底部导航栏。或许,导航栏这种常见