当前位置:   article > 正文

uniapp的两个跳转方式_uni.switchtab

uni.switchtab

uniapp内置多种跳转方式,我这里介绍两个最常用的跳转,uni.navigateTo和uni.switchTab,前者为跳转到非TabBar页面,后者为跳转到TabBar页面,所谓TabBar就是底部导航栏配置的页面,例如下方的index.vue。

在pages.json中

  1. "tabBar": {
  2. "list": [{
  3. "pagePath": "pages/index/index",
  4. "text": "首页",
  5. "iconPath": "static/buliangzhu.png",
  6. "selectedIconPath": "static/zhuyeliang.png"
  7. },
  8. {
  9. "pagePath": "pages/user/user",
  10. "text": "个人中心",
  11. "iconPath": "static/user2.png",
  12. "selectedIconPath": "static/user1.png"
  13. }
  14. ]
  15. },

配置成导航栏的页面无法通过navigateTo方法跳转,只能通过switchTab方法。

一、navigateTo

例如我们需要在index.vue页面里面跳转到test1.vue,test1是非tabBar页面,就给需要绑定事件的按钮添加点击事件。再在methods里面编写方法,调用uni.navigateTo

  1. <!-- index.vue -->
  2. <template>
  3. <view class="home ">
  4. <button @click="toTest1"></button>
  5. </view>
  6. </template>
  7. <script>
  8. export default {
  9. data() {
  10. return {
  11. }
  12. },
  13. methods: {
  14. toTest1() {
  15. uni.navigateTo({
  16. url:'/pages/test1/test1'
  17. })
  18. }
  19. }
  20. }
  21. </script>
二、switchTab

这里我们在test1页面中想点击按钮跳转到index页面

  1. <template>
  2. <view class="home ">
  3. <button @click="toIndex"></button>
  4. </view>
  5. </template>
  6. <script>
  7. export default {
  8. data() {
  9. return {
  10. }
  11. },
  12. methods: {
  13. toIndex() {
  14. uni.switchTab({
  15. url:'/pages/index/index'
  16. })
  17. }
  18. }
  19. }
  20. </script>

如果要从成功后跳转到tabBar页面只能用switchTab

  1. success: (res) => {
  2. console.log(res.data.status, '状态');
  3. if (res.data.status == 1) {
  4. this.msgType = 'success'
  5. this.messageText = '更新成功'
  6. this.$refs.message.open()
  7. uni.switchTab({
  8. url: '/pages/index/index'
  9. })
  10. } else {
  11. this.msgType = 'error'
  12. this.messageText = '更新失败'
  13. this.$refs.message.open()
  14. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Guff_9hys/article/detail/783594
推荐阅读
相关标签
  

闽ICP备14008679号