溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

微信小程序學習筆記之跳轉頁面、傳遞參數獲得數據操作圖文詳解

發(fā)布時間:2020-09-23 06:33:04 來源:腳本之家 閱讀:175 作者:李維山 欄目:web開發(fā)

本文實例講述了微信小程序學習筆記之跳轉頁面、傳遞參數獲得數據操作。分享給大家供大家參考,具體如下:

前面一篇介紹了微信小程序表單提交與PHP后臺數據交互處理?,F在需要實現點擊博客標題或縮略圖,跳轉到博客詳情頁面。

微信小程序學習筆記之跳轉頁面、傳遞參數獲得數據操作圖文詳解

開始想研究一下微信小程序的web-view組件跳轉傳參,把網頁嵌入到小程序,結果看到官方文檔的一句話打消了念頭,因為沒有認證......

微信小程序學習筆記之跳轉頁面、傳遞參數獲得數據操作圖文詳解

【方法一 使用navigator組件跳轉傳參】

前臺博客列表頁面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
  })
 }
})

其他與方法一相同,可實現與方法一相同跳轉傳參。

希望本文所述對大家微信小程序開發(fā)有所幫助。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI