前言
淘寶小程序(商家應(yīng)用)提供了很多跳轉(zhuǎn)的api,比如跳轉(zhuǎn)淘寶店鋪首頁(yè),跳轉(zhuǎn)商品詳情頁(yè)面,跳轉(zhuǎn)BC客服頁(yè)面等等。雖然官方不支持跳轉(zhuǎn)h5頁(yè),但提供了web-view組件以實(shí)現(xiàn)跳轉(zhuǎn)h5功能,以下是本人整理的跳轉(zhuǎn)各種頁(yè)面的方法。
如果有更多方法或整理有誤,歡迎評(píng)論補(bǔ)充指正。
跳轉(zhuǎn)應(yīng)用內(nèi)某個(gè)頁(yè)面
用官方api my.navigateTo
my.navigateTo({ url: "/pages/home/index" })}
重定向應(yīng)用內(nèi)某個(gè)頁(yè)面
my.redirectTo({ url: '/pages/home/index' })
返回上一頁(yè)
my.navigateBack()
跳轉(zhuǎn)/跳回某個(gè)小程序
跳轉(zhuǎn)某個(gè)小程序 ,需要目標(biāo)小程序appId。
跳轉(zhuǎn)回上一個(gè)小程序,只有從上一個(gè)小程序跳轉(zhuǎn)到當(dāng)前小程序時(shí)才會(huì)調(diào)用成功。
my.navigateToMiniProgram({
appId: '2017072607907880', //目標(biāo)小程序appId
extraData: {
"data1": "test"
}, //傳遞給目標(biāo)小程序的數(shù)據(jù),目標(biāo)小程序可在 App.onLaunch(),App.onShow() 中獲取到這份數(shù)據(jù)
path: '', //打開的頁(yè)面路徑,如果為空則打開首頁(yè)
success: (res) => {
console.log(JSON.stringify(res))
},
fail: (res) => {
console.log(JSON.stringify(res))
}
});
my.navigateBackMiniProgram({
extraData: {
"data1": "test"
}, //傳遞給目標(biāo)小程序的數(shù)據(jù),目標(biāo)小程序可在 App.onLaunch(),App.onShow() 中獲取到這份數(shù)據(jù)
success: (res) => {
console.log(JSON.stringify(res))
},
fail: (res) => {
console.log(JSON.stringify(res))
}
});
跳轉(zhuǎn)h5頁(yè)
淘寶小程序不支持跳轉(zhuǎn)外部H5頁(yè)面,不過(guò)可以用官方web-view組件實(shí)現(xiàn)跳轉(zhuǎn)H5頁(yè)功能
// webLink/index.axml
<view>
<web-view src="{{src}}"></web-view>
</view>
// webLink/index.js
Page({
data: {
src : ''
},
onLoad(props) {
// 參數(shù)link為h5鏈接
let {link} = props
this.setData({
src: link
});
},
});
// utils.js
// 跳轉(zhuǎn)webview頁(yè)面 , url為h5頁(yè)面鏈接
my.navigateTo({
url: `/pages/webLink/index?link=${url}`
});
跳轉(zhuǎn)到淘寶店鋪首頁(yè)
用官方api my.tb.navigateToTaobaoPage ,需要店鋪id
//實(shí)例代碼
my.tb.navigateToTaobaoPage({
appCode:'shop', //指定打開店鋪?lái)?yè)
appParams:{
shopId:"180175726",
weexShopTab:"shopindexbar",
weexShopSubTab:"shopindex"
},
success: (res) => {
my.alert({ content: "success - " + res.success })
},
fail: (res) => {
my.alert({ content: "fail - " + res.error })
}
});
跳轉(zhuǎn)詳情頁(yè)
用官方api my.tb.openDetail ,需要商品id
my.tb.openDetail ({
itemId: "576308890723", //商品id
success: (res) => {
my.alert({ content: "success" });
},
fail: (res) => {
my.alert({ content: "fail - " + res.error });
},
});
跳轉(zhuǎn)BC客服頁(yè)
跳轉(zhuǎn)到淘寶店鋪客服用api my.tb.openMessage
my.tb.openMessage({
sellerNick:"商家測(cè)試賬號(hào)", //賣家昵稱,就是淘寶店鋪名稱
success: (res) => {
console.log(res);
},
fail: (res) => {
console.log(res);
},
})