微信小程序 navigator 跳转组件的正确使用姿势

在网上看了一堆关于点击触发页面跳转的实现方法,方法很多不赘述。但是我却一个都没成功实现(毕竟从零开始折腾 Javascript 和 html)。在看了不知道多少教程后,才成功实现点击跳转。本文利用的是官方的 navigator 组件。

这里我使用了WeUI for 小程序里的基础组件的 panel 的代码作为基础。这里贴上 panel 的原始代码。

panel.js

var base64 = require("../images/base64");

Page({
    onLoad: function(){
        this.setData({
            icon20: base64.icon20,
            icon60: base64.icon60
        });
    }
});

panel.wxml

<view class="page">
    <view class="page__hd">
        <view class="page__title">Panel</view>
        <view class="page__desc">面板</view>
    </view>
    <view class="page__bd">
        <view class="weui-panel weui-panel_access">
            <view class="weui-panel__hd">图文组合列表</view>
            <view class="weui-panel__bd">
                <navigator url="" class="weui-media-box weui-media-box_appmsg" hover-class="weui-cell_active">
                    <view class="weui-media-box__hd weui-media-box__hd_in-appmsg">
                        <image class="weui-media-box__thumb" src="{{icon60}}" />
                    </view>
                    <view class="weui-media-box__bd weui-media-box__bd_in-appmsg">
                        <view class="weui-media-box__title">标题一</view>
                        <view class="weui-media-box__desc">由各种物质组成的巨型球状天体,叫做星球。星球有一定的形状,有自己的运行轨道。</view>
                    </view>
                </navigator>
                <navigator url="" class="weui-media-box weui-media-box_appmsg" hover-class="weui-cell_active">
                    <view class="weui-media-box__hd weui-media-box__hd_in-appmsg">
                        <image class="weui-media-box__thumb" src="{{icon60}}" />
                    </view>
                    <view class="weui-media-box__bd weui-media-box__bd_in-appmsg">
                        <view class="weui-media-box__title">标题二</view>
                        <view class="weui-media-box__desc">由各种物质组成的巨型球状天体,叫做星球。星球有一定的形状,有自己的运行轨道。</view>
                    </view>
                </navigator>
            </view>
            <view class="weui-panel__ft">
                <view class="weui-cell weui-cell_access weui-cell_link">
                    <view class="weui-cell__bd">查看更多</view>
                    <view class="weui-cell__ft weui-cell__ft_in-access"></view>
                </view>
            </view>
        </view>
        <view class="weui-panel weui-panel_access">
            <view class="weui-panel__hd">文字组合列表</view>
            <view class="weui-panel__bd">
                <view class="weui-media-box weui-media-box_text">
                    <view class="weui-media-box__title weui-media-box__title_in-text">标题一</view>
                    <view class="weui-media-box__desc">由各种物质组成的巨型球状天体,叫做星球。星球有一定的形状,有自己的运行轨道。</view>
                </view>
                <view class="weui-media-box weui-media-box_text">
                    <view class="weui-media-box__title weui-media-box__title_in-text">标题二</view>
                    <view class="weui-media-box__desc">由各种物质组成的巨型球状天体,叫做星球。星球有一定的形状,有自己的运行轨道。</view>
                </view>
            </view>
            <view class="weui-panel__ft">
                <view class="weui-cell weui-cell_access weui-cell_link">
                    <view class="weui-cell__bd">查看更多</view>
                    <view class="weui-cell__ft weui-cell__ft_in-access"></view>
                </view>
            </view>
        </view>
        <view class="weui-panel">
            <view class="weui-panel__hd">小图文组合列表</view>
            <view class="weui-panel__bd">
                <view class="weui-media-box weui-media-box_small-appmsg">
                    <view class="weui-cells weui-cells_in-small-appmsg">
                        <navigator url="" class="weui-cell weui-cell_access" hover-class="weui-cell_active">
                            <view class="weui-cell__hd"><image src="{{icon20}}" style="width: 20px;height: 20px;margin-right: 5px" /></view>
                            <view class="weui-cell__bd weui-cell_primary">
                                <view>文字标题</view>
                            </view>
                            <view class="weui-cell__ft weui-cell__ft_in-access"></view>
                        </navigator>
                        <navigator url="" class="weui-cell weui-cell_access" hover-class="weui-cell_active">
                            <view class="weui-cell__hd"><image src="{{icon20}}" style="width: 20px;height: 20px;margin-right: 5px" /></view>
                            <view class="weui-cell__bd weui-cell_primary">
                                <view>文字标题</view>
                            </view>
                            <view class="weui-cell__ft weui-cell__ft_in-access"></view>
                        </navigator>
                    </view>
                </view>
            </view>
        </view>
        <view class="weui-panel">
            <view class="weui-panel__hd">文字列表附来源</view>
            <view class="weui-panel__bd">
                <view class="weui-media-box weui-media-box_text">
                    <view class="weui-media-box__title weui-media-box__title_in-text">标题一</view>
                    <view class="weui-media-box__desc">由各种物质组成的巨型球状天体,叫做星球。星球有一定的形状,有自己的运行轨道。</view>
                    <view class="weui-media-box__info">
                        <view class="weui-media-box__info__meta">文字来源</view>
                        <view class="weui-media-box__info__meta">时间</view>
                        <view class="weui-media-box__info__meta weui-media-box__info__meta_extra">其它信息</view>
                    </view>
                </view>
            </view>
        </view>
    </view>
</view>

看起来有点乱,那么我把图标和面板的展示的类似代码删除,现在是这样。

panel.js

Page({

});

panel.wxml

<view class="page">
    <view class="page__bd">
        <view class="weui-panel weui-panel_access">
            <view class="weui-panel__hd">图文组合列表</view>
            <view class="weui-panel__bd">
                <navigator url="" class="weui-media-box weui-media-box_appmsg" hover-class="weui-cell_active">
                    <view class="weui-media-box__hd weui-media-box__hd_in-appmsg">
                        <image class="weui-media-box__thumb" src="" />
                    </view>
                    <view class="weui-media-box__bd weui-media-box__bd_in-appmsg">
                        <view class="weui-media-box__title">标题一</view>
                        <view class="weui-media-box__desc">由各种物质组成的巨型球状天体,叫做星球。星球有一定的形状,有自己的运行轨道。</view>
                    </view>
                </navigator>
                <navigator url="" class="weui-media-box weui-media-box_appmsg" hover-class="weui-cell_active">
                    <view class="weui-media-box__hd weui-media-box__hd_in-appmsg">
                        <image class="weui-media-box__thumb" src="" />
                    </view>
                    <view class="weui-media-box__bd weui-media-box__bd_in-appmsg">
                        <view class="weui-media-box__title">标题二</view>
                        <view class="weui-media-box__desc">由各种物质组成的巨型球状天体,叫做星球。星球有一定的形状,有自己的运行轨道。</view>
                    </view>
                </navigator>
            </view>
            <view class="weui-panel__ft">
                <view class="weui-cell weui-cell_access weui-cell_link">
                    <view class="weui-cell__bd">查看更多</view>
                    <view class="weui-cell__ft weui-cell__ft_in-access"></view>
                </view>
            </view>
        </view>
    </view>
</view>

那么此时默认别的条件都妥当了,比如每个 xx.js 都有 Page({

}); ,正确引用了 weui.wxss ,每个页面都在 app.json 注册过。

那么此时我定义,在 panel 所在的文件夹中,还有一个名为 media 的文件夹,其中有两个页面,分别是 1 和 2(这两个页面要在 app.json 注册过)。

那么我在 navigator url 中加入跳转目标页面相对于原始页面的相对路径,那么两个页面的路径就分别是

media/1
media/2

那么将跳转路径写入 panel.wxml (红色加粗)

panel.wxml

<view class="page">
    <view class="page__bd">
        <view class="weui-panel weui-panel_access">
            <view class="weui-panel__hd">图文组合列表</view>
            <view class="weui-panel__bd">
                <navigator url="media/1" class="weui-media-box weui-media-box_appmsg" hover-class="weui-cell_active">
                    <view class="weui-media-box__hd weui-media-box__hd_in-appmsg">
                        <image class="weui-media-box__thumb" src="" />
                    </view>
                    <view class="weui-media-box__bd weui-media-box__bd_in-appmsg">
                        <view class="weui-media-box__title">标题一</view>
                        <view class="weui-media-box__desc">由各种物质组成的巨型球状天体,叫做星球。星球有一定的形状,有自己的运行轨道。</view>
                    </view>
                </navigator>
                <navigator url="media/2" class="weui-media-box weui-media-box_appmsg" hover-class="weui-cell_active">
                    <view class="weui-media-box__hd weui-media-box__hd_in-appmsg">
                        <image class="weui-media-box__thumb" src="" />
                    </view>
                    <view class="weui-media-box__bd weui-media-box__bd_in-appmsg">
                        <view class="weui-media-box__title">标题二</view>
                        <view class="weui-media-box__desc">由各种物质组成的巨型球状天体,叫做星球。星球有一定的形状,有自己的运行轨道。</view>
                    </view>
                </navigator>
            </view>
            <view class="weui-panel__ft">
                <view class="weui-cell weui-cell_access weui-cell_link">
                    <view class="weui-cell__bd">查看更多</view>
                    <view class="weui-cell__ft weui-cell__ft_in-access"></view>
                </view>
            </view>
        </view>
    </view>
</view>

接下来就在 panel.js 添加代码(红色加粗)

panel.js

Page({
  onLoad: function (options) {
    this.setData({
      title: options.title
    })
  }
})

这时编译一下然后运行,就可以点击跳转页面了!


相关阅读:微信小程序调用 WeUI 的正确姿势—Thregren