微信小程序开发中遇到的问题(一)

微信小程序开发中遇到的问题(一):

1、wxml中设置的值,js获取

前台wxml的中设置了一个id属性,且值为123


---

后台js接值

getId:function(e){
  let id=e.currentTarget.dataset.id; 
  console.log(id)  //显示123
}

2、小程序的某个js跳转到另一个页面怎么传值,另一页面怎么接值

index.js中传值

wx.redirectTo({
   url:'/pages/test/test?id=456'
})

test.js中接值

Page({
   data:{
      sid:'default'
   },
   onLoad:function(options){
      this.setData({
         sid:options.id  //把跳转带过来的id赋给声明的变量sid
      })
   }
})

3、定义全局变量,并且全局变量在其他页面中运用

app.js中

App({
   data:{
      httpurl:'http://111.222.333.444:8888'
   }
})

其他js中

const app=getApp();
Page({
   Data:{
     htpurl:app.data.httpurl
   }
})

4、小程序跳转页面的几种常用方法

(1)wx.navigateTo

保留当前页面,跳转到应用内的某个页面,用wx.navigateBack可返回到原页面

注意:调用该方法的页面会被加入堆栈,可通过getCurrentPages()获取当前页面栈,决定需要返回几层

//跳转
wx.navigateTo({
   ur:'page/test/test' //需要注意的是,每新增一个模块,就需要在app.json中声明
})
//返回
wx.navigateBack({
   delta:2  //返回第2层堆栈
})

(2)wx.redirectTo

关闭当前页面,跳转到应用内的某个页面

wx.redirectTo({
   url:'page/test/test'
})

(3)wx.switchTab

跳转到tabBar页面(在app.json中注册过的taBar底部页面),同时关闭其他飞tabBar页面

wx.switchTab({
   url:'page/tabbar/tabbar'
})

(4)wx.reLanch

关闭所有页面,打开到应用内的某个页面

x.reLanch({
   url:'page/test/test'
})

(5)wxml页面组件跳转,通过设置open-type属性指明页面跳转方法

新页面  //默认navigator跳转
新页面 //redirect跳转
新页面 //switchTab跳转
新页面 //reLanch 跳转
新页面 //返回上级页面

 

你可能感兴趣的:(微信小程序)