:navigator方式:
要想通過 navigator 實現頁面跳轉我們要用到url屬性。
open-type屬性可用值(直接上官網截圖):
注意點:不能帶頁面后綴文件“.wxml”,帶了后不能跳轉。
錯的方式:
<navigator url="/pages/fgclass/meateclass/meateclass.wxml">熱門推薦頁面跳到肉類頁</navigator>
對的方式:
<navigator url="/pages/fgclass/meateclass/meateclass">熱門推薦頁面跳轉</navigator>
2.頁面跳轉傳參:
<navigator url="/pages/fgclass/meateclass/meateclass?param=longdb">熱門推薦頁面跳轉傳參數
</navigator>
被跳到的頁面接收參數:
只要我們在url中定義了param參數,在被跳轉頁面里就能在 onload周期函數的參數options里拿到。
取出參數值
二:API方式(官網:https://developers.weixin.qq.com/miniprogram/dev/api/route/wx.navigateTo.html):
Api跳轉方法列表:
wx.navigateTo()保留當前??,跳轉到應?內的某個??。但是不能跳到 tabbar ??
wx.switchTap()跳轉到 tabBar ??,并關閉其他所有? tabBar ??
wx.reLaunch()關閉所有??,打開到應?內的某個??
wx.redirectTo()關閉當前??,跳轉到應?內的某個??。但是不允許跳轉到 tabbar ??
wx.navigateBack()關閉當前??,返回上???或多級??
官網截圖
1.API跳轉頁面不帶參數:
/** api跳轉**/
bindNavigateto:function(){
url: '/pages/fgclass/meateclass/meateclass',
})
},
2:API跳轉頁面帶參數:
/** api跳轉**/
bindNavigateto:function(){
wx.navigateTo({
//url: '/pages/fgclass/meateclass/meateclass.wxml',
url: '/pages/fgclass/meateclass/meateclass?param=apijump',
})
},
在meateclass.js里這樣接收參數:
/**
* 生命周期函數--監聽頁面加載
*/
onLoad: function (options) {
console.log(options);
this.setData({
meateparater:options.param,
})
},
注意點:不能帶頁面后綴文件“.wxml”,帶了后不能跳轉。
總結:
以上就是小程序的兩種跳轉方式,是不是很簡單?另外的幾個跳轉api就不寫啦。
謝謝瀏覽,謝謝點贊和評論,覺得對你有用的就關注下唄,我也會關注你的。
我是只說代碼的大餅。
一定離不開 Vue Router 來管理你的應用路由。
而 router-link 組件就是 Vue Router 提供給我們的秘密武器,它可以讓你輕松實現頁面跳轉,并擁有豐富的功能和靈活的配置。
今天,我們就來深入了解一下 router-link 的用法和奧秘!
1. 基礎用法:
router-link 組件接受一個 to 屬性,用于指定跳轉的目標路由路徑。簡單來說,你只需要將 router-link 組件包裹在需要跳轉的鏈接文本或元素上,就可以實現頁面跳轉了。
示例代碼:
html
<template>
<div>
<router-link to="/">首頁</router-link>
<router-link to="/about">關于我們</router-link>
</div>
</template>
<script>
export default {
// ...
};
</script>
2. 鏈接文本和元素:
默認情況下,router-link 組件會將 to 屬性的值作為鏈接文本。可以使用 router-link 組件的 tag 屬性指定其他元素標簽,例如 a、button 等。
示例代碼:
html
<template>
<div>
<!-- 使用 a 標簽 -->
<router-link to="/" tag="a">首頁</router-link>
<!-- 使用 button 標簽 -->
<router-link to="/about" tag="button">關于我們</router-link>
</div>
</template>
3. 傳遞參數:
router-link 組件支持傳遞參數,可以通過 params 或 query 屬性進行傳遞。
示例代碼:
html
<template>
<div>
<!-- 使用 params 傳遞參數 -->
<router-link :to="{ name: 'user', params: { id: 1 } }">用戶詳情</router-link>
<!-- 使用 query 傳遞參數 -->
<router-link :to="{ path: '/search', query: { keyword: 'vue' } }">搜索</router-link>
</div>
</template>
4. 自定義激活樣式:
router-link 組件提供了 active-class 屬性,用于自定義當前激活路由的樣式。
示例代碼:
html
<template>
<div>
<router-link to="/" active-class="active">首頁</router-link>
</div>
</template>
<style scoped>
.active {
color: red;
}
</style>
5. router-link 組件的解析:
router-link 組件最終會被渲染成一個 <a> 標簽,并使用 Vue Router 的路由匹配邏輯來處理點擊事件。
源碼解析:
javascript
// router-link.js
import { h } from 'vue';
export default {
name: 'RouterLink',
props: {
to: {
type: [String, Object],
required: true
}
},
setup(props, { attrs }) {
const { href, target, rel, onClick } = attrs;
const resolved = resolveComponent(props.to);
const active = isActive(resolved.path, props.to);
return () => h('a', {
href: href || resolved.href,
target: target,
rel: rel,
onClick: (e) => onClick && onClick(e),
onMousedown: () => handleLink(resolved),
}, [props.children]);
}
};
希望這篇文章能幫助你更好地了解和使用 router-link 組件,讓你的 Vue Router 路由管理更加高效便捷!
歡迎在評論區分享你的經驗和困惑,一起探討學習,共同進步!
#Java后端需要學習哪些技術#
<script src="https://lf3-cdn-tos.bytescm.com/obj/cdn-static-resource/tt_player/tt.player.js?v=20160723"></script>
超級文本標簽 a
屬性 href 具體的資源位置
target 是否新打開一個頁面進行跳轉,默認不打開 _self _top
新打開空白頁進行跳轉 _blank
應用
1、跳轉到具體資源
url 網絡資源
path 本地資源
*請認真填寫需求信息,我們會在24小時內與您取得聯系。