微信小程序swiper点击轮播图实现跳转;其他页面跳转不成功的原因

微信小程序swiper点击轮播图实现跳转

用navigator的问题

昨晚写一个小程序的首页,涉及到轮播图,而我将所有的图片以数组的形式 放在js中的,官方文档只给出了navigator的超链接,但是我当时使用时整个页面会白一下,再跳转,会给人一种不舒服的感觉,于是我决定继续修改,改善代码在最终版::

第一次代码如下,为找到原因:

 <swiper class="index-banner" indicator-dots="{{true}}" circular="{{true}}"
    autoplay="{{true}}" interval="{{2000}}" duration="{{500}}">
    <block wx:for="{{imgUrls}}" wx:key="{{item}}">
     <navigator class="index-menu-item"  url="{{item.url}}">
      <swiper-item >
        <image src="{{item.icon}}" mode="aspectFill"  class="slide-image"/> 
      </swiper-item>
      </navigator>
    </block>
  </swiper>

轮播图跳转最终版

写了一个函数changeURL,xtml如下:

<swiper class="index-banner" indicator-dots="{{true}}" circular="{{true}}"
    autoplay="{{true}}" interval="{{2000}}" duration="{{500}}">
    <block wx:for="{{imgUrls}}" wx:key="{{item}}">
      <swiper-item  data-url="{{item.url}}" bindtap="changeURL">
        <image src="{{item.icon}}" mode="aspectFill"  class="slide-image"/> 
      </swiper-item>
    </block>
  </swiper>

js如下,其中e.currentTarget.dataset.url即是data-url="{{item.url}}"传过来的参数:

changeURL: function (e) {
    wx.navigateTo({
      url: `${e.currentTarget.dataset.url}`,
      success: function (res) {
        // success
      },
      fail: function () {
        // fail
      },
      complete: function () {
        // complete
      }
    })
  },

微信小程序页面跳转不成功的原因

问题简述

在写一个navigator点击跳转时,无法跳转到index和item的页面,神奇的是可以跳转到当前页面,GAMEOVER,相当于没跳转。所以对navigator产生了一些好奇,去查看了官方文档,文档提示:

1.需要用户确认跳转 从 2.3.0 版本开始,在跳转至其他小程序前,将统一增加弹窗,询问是否跳转,用户确认后才可以跳转其他小程序。如果用户点击取消,则回调 fail cancel。
2.每个小程序可跳转的其他小程序数量限制为不超过 10 个 从 2.4.0 版本以及指定日期(具体待定)开始,开发者提交新版小程序代码时,如使用了跳转其他小程序功能,则需要在代码配置中声明将要跳转的小程序名单,限定不超过 10 个,否则将无法通过审核。该名单可在发布新版时更新,不支持动态修改。配置方法详见 配置。调用此接口时,所跳转的 appId 必须在配置列表中,否则回调 fail appId “${appId}” is not in navigateToMiniProgramAppIdList

这两个都不符合我的情况,后查看了其他教程,错误定位成功。

原因概述

  • item跳转不成功

要跳转的路径未在app.js里注册过,或者路径写错了

  • index跳转不成功

要跳转的路径位于TabBar中,必须使用wx.switchTab 来跳转界面

  • 6
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
1. 在wxml文件中添加轮播图组件和按钮组件,例如: ```html <swiper class="swiper-container" indicator-dots="{{indicatorDots}}" autoplay="{{autoplay}}" interval="{{interval}}" duration="{{duration}}"> <swiper-item> <image src="{{imageUrls[0]}}" /> </swiper-item> <swiper-item> <image src="{{imageUrls[1]}}" /> </swiper-item> <swiper-item> <image src="{{imageUrls[2]}}" /> </swiper-item> </swiper> <button class="btn" bindtap="handleClick">点击跳转</button> ``` 2. 在js文件中定义轮播图组件的属性和点击事件处理函数,例如: ```javascript Page({ data: { imageUrls: [ 'http://img01.tooopen.com/images/20150828/sy_140703593077.jpg', 'http://img06.tooopen.com/images/20160818/tooopen_sy_175866434296.jpg', 'http://img06.tooopen.com/images/20160818/tooopen_sy_175833047715.jpg' ], indicatorDots: true, autoplay: true, interval: 5000, duration: 1000 }, handleClick: function() { wx.navigateTo({ url: '/pages/detail/detail' }) } }) ``` 3. 在点击事件处理函数中使用wx.navigateTo方法跳转到目标页面,例如: ```javascript wx.navigateTo({ url: '/pages/detail/detail' }) ``` 其中,'/pages/detail/detail'是目标页面的路径。注意,目标页面必须在app.json文件中注册过。 4. 在目标页面的wxml文件中展示跳转后的内容,例如: ```html <view class="detail">这是详情页</view> ``` 5. 在app.json文件中注册目标页面,例如: ```json { "pages": [ "pages/index/index", "pages/detail/detail" ], "window": { "backgroundTextStyle": "light", "navigationBarBackgroundColor": "#fff", "navigationBarTitleText": "WeChat", "navigationBarTextStyle": "black" } } ``` 其中,"pages/detail/detail"是目标页面的路径。
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值