我是靠谱客的博主 凶狠眼睛,这篇文章主要介绍uni-app 点击footer导航实现样式的切换和路由的跳转,现在分享给大家,希望可以做个参考。

在这里插入图片描述
需求:
点击切换样式并跳转路由

思路:
footer是五个主页面的公共组件,在footer组件跳转路由,在路由组件通过父子传参 current_ind 动态修改样式

工作台主页面组件

复制代码
1
2
3
4
5
6
<template> <view> <Footer :current_ind='2'></Footer> </view> </template>

footer.vue

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
<template> <view> <view v-for="(item,index) in footerList" :key="index"> <view @click="handleChangeCurrentPage(item)"> <text :class="[item.icon, current_ind === item.footer_ind ? 'current-page' : '']" ></text> <p :class="current_ind === item.footer_ind ? 'current-page' : ''">{{item.name}}</p> </view> </view> </view> </template> <script> export default { props: { current_ind: { type: Number, default: 0 } }, data() { return { footerList: [ { icon: 'icon-xiaoxi', name: '消息', footer_ind: 0, page_route: '/pages/template/news/index' }, { icon: 'icon-tongxunlu', name: '通讯录', footer_ind: 1, page_route: '/pages/template/mail-list/index' }, { icon: 'icon-gongzuotai', name: '工作台', footer_ind: 2, page_route: '/pages/template/stage/index' }, { icon: 'icon-xuexi_nor', name: '成长学院', footer_ind: 3, page_route: '/pages/template/growth-college/index' }, { icon: 'icon-wenjian', name: '文档', footer_ind: 4, page_route: '/pages/template/document/index' } ] } }, methods: { handleChangeCurrentPage(item) { uni.navigateTo({ url: item.page_route }) } } } </script> <style> .current-page{ color: #6487F8; } </style>

开发过程中需要注意的一些零碎知识点

  1. 动态切换类名
    <text class="iconfont " :class="[item.icon, current_ind === item.footer_ind ? 'current-page' : '']" ></text> 俩个动态类名使用数组

  2. watch 监听事件,只有在值改变时才可以起到监听,有俩个参数 newVal, oldVal,分别是修改后和修改前

  3. this 箭头函数指向,如果是下面代码,会报错 footerList is undefined,换成current_ind: function (newVal, oldVal) {} 可以解决报错

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
watch: { current_ind: (newVal, oldVal) => { this.footerList.forEach(item => { if (newVal === item.footer_ind) { uni.navigateTo({ url: item.page_route }) } }) } },

最后

以上就是凶狠眼睛最近收集整理的关于uni-app 点击footer导航实现样式的切换和路由的跳转的全部内容,更多相关uni-app内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(72)

评论列表共有 0 条评论

立即
投稿
返回
顶部