当前位置:网站首页>微信小程序中的路由跳转
微信小程序中的路由跳转
2022-07-07 04:02:00 【richest_qi】
文章目录
前情提要
微信小程序中支持路由跳转的常用API有,
wx.navigateTo()
,保留当前页面,跳转到应用中的某个页面。但是,不能跳转到tabbar页面。wx.redirectTo()
,关闭当前页面,跳转到应用内的某个页面。wx.reLaunch()
,关闭所有页面,打开应用中的某个页面。wx.switchTab()
,跳转到tabbar页面,并关闭其他所有非tabbar页面。
小程序项目
代码涉及的主要文件有:
- app.json
- app.wxss
- pages/home/home.wxml
- pages/home/home.json
- pages/camera/camera.wxml
- pages/camera/camera.json
- pages/user/user.wxml
- pages/user/user.json
- pages/login/login.wxml
- pages/login/login.json
- pages/login/login.wxss
- pages/login/login.js
主体
app.json
{
"pages": [
"pages/home/home",
"pages/camera/camera",
"pages/user/user",
"pages/login/login"
],
"window": {
"navigationBarBackgroundColor": "#0149af",
"navigationBarTitleText": "首页",
"navigationBarTextStyle": "white"
},
"tabBar": {
"color": "#0149af",
"selectedColor": "#0149af",
"list": [{
"pagePath": "pages/home/home",
"text": "首页",
"iconPath": "/static/images/tabIcons/homepage.png",
"selectedIconPath": "/static/images/tabIcons/homepage_fill.png"
},{
"pagePath": "pages/camera/camera",
"text": "相册",
"iconPath": "/static/images/tabIcons/camera.png",
"selectedIconPath": "/static/images/tabIcons/camera_fill.png"
},{
"pagePath": "pages/user/user",
"text": "个人中心",
"iconPath": "/static/images/tabIcons/people.png",
"selectedIconPath": "/static/images/tabIcons/people_fill.png"
}]
},
"style": "v2",
"sitemapLocation": "sitemap.json"
}
app.wxss
.container{
margin: 40rpx;
padding: 40rpx;
height: 600rpx;
background:#a3c8f8;
border-radius: 6rpx;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
font-size: 48rpx;
color: #fff;
}
首页(pages/home)
pages/home/home.wxml
<view class="container">
首页
</view>
相册(pages/camera)
pages/camera/camera.wxml
<view class="container">
相册
</view>
pages/camera/camera.json
{
"navigationBarTitleText": "相册"
}
个人中心(pages/user)
pages/user/user.wxml
<view class="userContainer">
<image src="/static/images/vistor.png"></image>
<view class="name">游客</view>
<view class="text" bindtap="handleTap">去登录?</view>
</view>
pages/user/user.json
{
"navigationBarTitleText": "个人中心"
}
登录(pages/login)
pages/login/login.wxml
<view class="loginContainer">
<view class="title">登录</view>
<view class="list">
<view class="item">
<input type="text" data-type="username" bindinput="handleInput" placeholder="用户名" />
</view>
<view class="item">
<input type="text" password data-type="password" bindinput="handleInput" placeholder="密码" />
</view>
</view>
<button size="mini" bindtap="login">登录</button>
</view>
pages/login/login.json
{
"navigationBarTitleText": "登录"
}
pages/login/login.wxss
.loginContainer{
padding: 20rpx;
}
.loginContainer .title{
padding: 20rpx 0;
border-bottom: 1px solid #ddd;
font-weight: bold;
font-size: 32rpx;
}
.list{
margin: 40rpx auto 20rpx;
}
.item{
margin: 12rpx 0;
padding: 0 20rpx;
border: 1px solid #ddd;
border-radius: 6rpx;
}
.item input{
width: 100%;
height: 60rpx;
font-size: 28rpx;
}
button{
font-weight: normal;
font-size: 28rpx!important;
color: #fff;
background: #0149af;
}
pages/login/login.js
Page({
data:{
username:"",
password:""
},
handleInput(e){
const type = e.target.dataset.type;
this.setData({
[type]:e.detail.value
})
},
login(){
const {
username,password} = this.data;
if(!username){
wx.showToast({
title: '请输入您的用户名',
icon:'error'
})
return;
}
if(!password){
wx.showToast({
title: '请输入您的密码',
icon:'error'
})
return;
}
wx.showToast({
title: '登录成功',
success:() => {
setTimeout(() => {
wx.switchTab({
url: '/pages/user/user',
})
},300)
}
})
/** * 报错:navigateTo:fail can not navigateTo a tabbar page */
// wx.navigateTo({
// url: '/pages/user/user',
// })
}
})
相关链接
边栏推荐
- Causes and solutions of oom (memory overflow)
- At the age of 20, I got the ByteDance offer on four sides, and I still can't believe it
- ../ And/
- Flutter riverpod is comprehensively and deeply analyzed. Why is it officially recommended?
- Sqlmap tutorial (IV) practical skills three: bypass the firewall
- JS small exercise ---- time sharing reminder and greeting, form password display hidden effect, text box focus event, closing advertisement
- Fast quantitative, abbkine protein quantitative kit BCA method is coming!
- 【Liunx】进程控制和父子进程
- Convolutional neural network -- understanding of pooling
- JS small exercise
猜你喜欢
Convolutional neural network -- understanding of pooling
【leetcode】1020. Number of enclaves
Tencent's one-day life
IO流 file
BGP experiment (1)
身边35岁程序员如何建立起技术护城河?
Project practice five fitting straight lines to obtain the center line
After 95, the CV engineer posted the payroll and made up this. It's really fragrant
Nesting and splitting of components
Implementing data dictionary with JSP custom tag
随机推荐
Summary of customer value model (RFM) technology for data analysis
Detailed explanation of neo4j installation process
Implementing data dictionary with JSP custom tag
MobaXterm
How can a 35 year old programmer build a technological moat?
L'étape avancée du pointeur de langage C (haut de gamme) pour l'enroulement des cocons
在线直播系统源码,使用ValueAnimator实现view放大缩小动画效果
At the age of 20, I got the ByteDance offer on four sides, and I still can't believe it
三、高质量编程与性能调优实战 青训营笔记
Lm11 reconstruction of K-line and construction of timing trading strategy
Fast quantitative, abbkine protein quantitative kit BCA method is coming!
3、 High quality programming and performance tuning practical youth training camp notes
Wechat applet full stack development practice Chapter 3 Introduction and use of APIs commonly used in wechat applet development -- 3.10 tabbar component (I) how to open and use the default tabbar comp
Tumor immunotherapy research prosci Lag3 antibody solution
Stockage et pratique des données en langage C (haut niveau)
基于Flask搭建个人网站
Make a bat file for cleaning system garbage
【云原生】内存数据库如何发挥内存优势
Abnova immunohistochemical service solution
Introduction to abnova's in vitro mRNA transcription workflow and capping method