微信小程序 -订阅发布模式

图形展示:
代码展示:
  1. 安装模块 pubsub-js

    npm i pubsub-js --save

  2. 导入模块(在需要订阅发布的 js 页面内进行导入)

    import PubSub from 'pubsub-js'

注:在微信小程序中无法直接npm 下载 导入 的(安装一个就需要构建一次)

解决:菜单栏 --> 工具 --> 构建 npm 点击即可(会出现新的目录)

详情页:
  1. 绑定两个事件,用于在点击 prev(上一页) next(下一页) 时触发事件

wxml页面:

复制代码
<viewclass="musicControl">
    <text class="iconfont icon-iconsMusicyemianbofangmoshiShuffle"></text>
    <text
        class="iconfont icon-shangyishou"
        id="pre"
        bindtap="switchMusic"  //绑定事件
    ></text>
    <text
        class="iconfont {{isPlay ? "icon-zanting':'icon-bofang' } big"
        bindtap="musicPlay"  
    ></text>
    <text 
        class="iconfont icon-next" 
        id="next bindtap="switchMusic'  //绑定事件
    ></text>
    <text class="iconfont icon-iconsMusicyemianbofangmoshiplayList"></text>
</view>
  1. 触发事件,使用 PubSub 的 publish 方法进行发布

js页面:

复制代码
switchMusic(e){
    //是需要向歌曲列表页进行信息的发布
    const type = e.currentTarget.id;
    PubSub.publish("switchType",type);
},
  1. 在生命周期 onLoad 中 使用 PubSub 的 subscribe 方法对列表页发布的内容进行订阅

有两个参数:

参数一:要订阅的发布名称

参数二:回调函数,处理订阅的 发布信息内容

函数参数一 (msg):发布的对应名称 如:PubSub.publish("musicId",musicId);

函数参数二 (musicId):发布的对应参数 如:PubSub.publish("musicId",musicId);

复制代码
onLoad(options) {
    PubSub.subscribe("musicId",(msg, musicId) => {
        console.log("musicId:", musicId);
    });
},

列表页:

  1. 在生命周期 onLoad 中 使用 PubSub 的 subscribe 方法对详情页发布的内容进行订阅

有两个参数:

参数一:要订阅的发布名称

参数二:回调函数,处理订阅的 发布信息内容

函数参数一 (msg):发布的对应名称 如: PubSub.publish("switchType",type);

函数参数二 (type):发布的对应参数 如: PubSub.publish("switchType",type);

复制代码
onLoad(options) {
    PubSub.subscribe("switchType",(msg, type) => {
        let { index, recommendList } = this.data;
        
        // 控制边界
        if(type === "next") {
            index === recommendList.length -1 && (index = -1);
            // 下一首
            index += 1;
        } else {
            index === 0 && (index = recommendList.length);
            //上一首
            index -= 1;
        }
        console.log(index);
    });
},
  1. 处理完数据后,在下方使用 PubSub 的 publish 方法进行发布

    onLoad(options) {
    PubSub.subscribe("switchType",(msg, type) => {
    let { index, recommendList } = this.data;

    复制代码
         if(type === "next") {
             // 下一首
             index += 1;
         } else {
             //上一首
             index -= 1;
         }
         console.log(index);
         let musicId = recommendList[index].id;
         PubSub.publish("musicId",musicId); //将音乐id发布到详情页
         this.setData({
             index,
         });
     });

    },

相关推荐
CHU7290352 小时前
家政同城服务APP前端功能玩法解析
前端·小程序
weixin_lynhgworld2 小时前
硬核功能赋能运营,定制盲盒小程序解锁沉浸式抽盒体验
小程序
灵机一物4 小时前
灵机一物AI智能电商小程序-ReAct驱动的虚拟试穿智能体产品方案
人工智能·小程序
CHU7290354 小时前
剧本杀组车约玩小程序前端功能版块设计及玩法介绍
前端·小程序
chushiyunen5 小时前
pycharm创建桌面时间控件小程序
ide·小程序·pycharm
宁夏雨科网5 小时前
SaaS系统搭建宠物用品小程序可行吗
小程序·商城小程序·微信商城·制作小程序·宠物用品小程序·宠物用品商城
CHU72903516 小时前
直播逛购新体验——直播商城APP前端功能详解
前端·小程序
云起SAAS21 小时前
设备质保保修维修保质包换期查询H5抖音快手微信小程序看广告流量主开源
微信小程序·小程序
焦糖玛奇朵婷1 天前
盲盒小程序一站式开发
java·大数据·服务器·前端·小程序
CHU7290351 天前
潮玩解锁新方式!扭蛋机盲盒小程序前端功能玩法解析
小程序