当前位置: 移动技术网 > IT编程>开发语言>JavaScript > 微信小程序学习笔记之跳转页面、传递参数获得数据操作图文详解

微信小程序学习笔记之跳转页面、传递参数获得数据操作图文详解

2019年07月25日  | 移动技术网IT编程  | 我要评论

本文实例讲述了微信小程序学习笔记之跳转页面、传递参数获得数据操作。分享给大家供大家参考,具体如下:

前面一篇介绍了微信小程序表单提交与php后台数据交互处理。现在需要实现点击博客标题或缩略图,跳转到博客详情页面。

开始想研究一下微信小程序的组件跳转传参,把网页嵌入到小程序,结果看到官方文档的一句话打消了念头,因为没有认证......

【方法一 使用组件跳转传参】

前台博客列表页面data.wxml:(后台数据交互参考上一篇)

<view wx:for="{{artinfo}}" wx:for-item="artinfo">
  <view>
    <navigator url="/pages/detial/detial?article_id={{artinfo.article_id}}" >
     {{artinfo.article_title}}
    </navigator>
  </view>
  <navigator url="/pages/detial/detial?article_id={{artinfo.article_id}}" >
   <image src="{{artinfo.thumbnail}}"></image>
  </navigator>
</view>

前台博客详情页面detail.js:

page({
 onload: function (options) { //options:页面跳转所传的参数
  var that = this
  wx.request({
   url: 'https://www.msllws.top/getdata/detial',
   data: {
    'article_id': options.article_id
   },
   method: 'post',
   header: {
    'content-type': 'application/x-www-form-urlencoded'
   },
   success: function (res) {
    if (res.data.state == 1) {
     that.setdata({
      artinfo: res.data.data
     })
    }else{
     wx.showtoast({
      title: res.data.msg
     });
    }
   }
  })
 }
})

前台博客详情页面detail.wxml:

<view>{{artinfo.article_title}}</view>
<view>———————————————————————————</view>
<rich-text nodes="{{artinfo.article_content}}"></rich-text>

后台获取博客内容接口:(tp5)

 public function detial()
 { 
   $arr = array('state'=>0,'msg'=>'','data'=>array());
   $article_id = $_post['article_id'];
   if($article_id){
     $whe['article_id'] = $article_id;
     $artinfo = db('article')->field('`article_title`,`article_content`')->where($whe)->find();
     if($artinfo){
       $arr['state'] = 1;
       $arr['msg'] = 'success';
       $arr['data'] = $artinfo;
     }else{
       $arr['msg'] = '没有信息';
     }
   }else{
     $arr['msg'] = '参数错误';
   }
   echo json_encode($arr);die;
 }

实现效果如下:

【方法二 使用wx.navigateto api跳转传参】

前台博客列表页面data.wxml:

(data-xxx:自定义参数属性,catchtap:绑定点击事件)

<view wx:for="{{artinfo}}" wx:for-item="artinfo">
  <view data-article_id="{{artinfo.article_id}}" catchtap="showdetial">
     {{artinfo.article_title}}
   </view>
  <view data-article_id="{{artinfo.article_id}}" catchtap="showdetial">
   <image src="{{artinfo.thumbnail}}"></image>
  </view>
</view>

前台博客列表页面data.js:

page({
 onload: function () {
  var that = this
  wx.request({
   url: 'https://www.msllws.top/getdata',
   headers: {
    'content-type': 'application/json'
   },
   success: function (res) {
    that.setdata({
     artinfo: res.data
    })
   }
  })
 },
 showdetial: function (e) {
  var article_id = e.currenttarget.dataset.article_id;
  wx.navigateto({
   url: '/pages/detial/detial?article_id=' + article_id
  })
 }
})

其他与方法一相同,可实现与方法一相同跳转传参。

希望本文所述对大家微信小程序开发有所帮助。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网