微信小程序中一个界面中多个按钮如何跳转到不同界面

我的采用goTo多个按钮只能跳转到同一个界面,请用比较简单易懂的代码

不同按钮button标签通过bindtap属性绑定不同点击事件,然后每个触发事件里给不同的url地址,然后再把url传给跳转函数就可以了

比如:

<button id="1"  bindTap = 'gotoPage'/>; 
<button id="2" bindTap = 'gotoPage'/>

gotoPage: function(event){ 
  const number = event.target.id;//1或者2得到点击了按钮1或者按钮2 
  const url = "/pages/index/talkPage" + number;//得到页面url 
  wx.navigateTo({
    url: url, 
  }) 
}

img

给没个按钮设置一个标识属性,然后统一用一个跳转函数,根据每个按钮传入的标识属性,跳转不通的页面

微信里面绑定属性值,是用data- 后面是名称。
例如

然后在js里面可以拿到这个值。
goto:function(e){
let num = e.currentTarget.dataset.num;
//然后再控制根据num跳转到不同页面
switch(num){
case 1:
wx.navigateTo({url:''});
break;
case 2:
wx.navigateTo({url:''});
break;
}
}