首先是有這麼一個需求,從 主頁
-> 個人關注
-> 別人的主頁
, 主頁
這個模塊被打開了兩次
我拍拍胸脯對產品說:沒問題!css
這個需求是沒毛病的,就是當咱們從 別人的主頁
-> 個人關注
-> 主頁
的時候,個人信息變成了我剛剛打開的那我的的信息,這TM... html
不要慌,有些事情須要咱們 「摸索」 前端
在 mpvue 路由 Page A -> Page A -> Back 的時候,其實數據公用了一個對象,不知道爲何mpvue會這麼設計,在官方解決這個問題以前這裏引用了 @maimake的解決辦法vue
首先建立一個hack文件 /src/util/Hack.js
git
const pageDatas = {} export default { install(_Vue) { // 添加全局方法或屬性 _Vue.prototype.$isPage = function isPage() { return this.$mp && this.$mp.mpType === 'page' } _Vue.prototype.$pageId = function pageId() { let pid = null try { pid = this.$isPage() ? this.$mp.page.__wxWebviewId__ : null } catch (e) { } return pid } // 注入組件 _Vue.mixin({ methods: { stashPageData() { return { data: { ...this.$data } } }, restorePageData(oldData) { Object.assign(this.$data, oldData.data) }, }, onLoad() { if (this.$isPage()) { // 新進入頁面 Object.assign(this.$data, this.$options.data()) } }, onUnload() { if (this.$isPage()) { // 退出頁面,刪除數據 delete pageDatas[this.$pageId()] this.$needReloadPageData = true } }, onHide() { if (this.$isPage()) { // 將要隱藏時,備份數據 pageDatas[this.$pageId()] = this.stashPageData() } }, onShow() { if (this.$isPage()) { // 若是是後退回來的,拿出歷史數據來設置data if (this.$needReloadPageData) { const oldData = pageDatas[this.$pageId()] if (oldData) { this.restorePageData(oldData) } this.$needReloadPageData = false } } }, }) }, }
在main.js中引入 /src/main.js
github
import Vue from 'vue' import App from './App' import Hack from './utils/Hack' Vue.config.productionTip = false App.mpType = 'app' Vue.use(Hack) const app = new Vue(App) app.$mount()
測試後發現ok了,可是這只是一個臨時解決方案,治標不治本,仍是但願官方儘快解決。小程序
scroll-view
組件的一些問題其實吧,我並不推薦使用這個組件,也不是mpvue的問題,就是這個組件自己就不太好用。前端框架
在mpvue中使用下面的一些事件的時候是不會觸發的app
bindscrolltolower
bindscrolltoupper
bindscroll
解決方法你猜是什麼?就是把bind
去掉,哈哈哈框架
scrolltolower
scrolltoupper
scroll
然而,當 scroll-view
高度爲 100% 的時候 bindscrolltolower
又不執行了,解決方法是給外面的父元素加一些限制
<div class="cat-scroll"> <scroll-view style="height: 100%; width: 100%"> </<scroll-view> </div>
.cat-scroll { position: absolute; left: 0; right: 0; bottom: 0; top: 0; overflow: hidden; }
這樣就解決了,但但但但但但但可是,這樣鋪滿屏幕後和小程序的下拉刷新衝突了,致使下拉刷新不執行。WTF
回到咱們開頭說的不用 scroll-view
咱們用別的方法解決。首先咱們使用 scroll-view
的緣由通常就是作一個下拉加載更多麼,在小程序中頁面全局有一個滾動到底部的事件 onReachBottom
咱們只須要在這個事件裏面寫上拉加載更多就行了,和下拉刷新也不衝突。
export default { onPullDownRefresh(){ // 下拉刷新 }, onReachBottom(){ // 上拉加載更多 } }
在新版本的mpvue中支持了 slot
這個功能,可是我勸你們不要用。問題不少,父子之間傳值有各類問題,我也沒找到很好的解決辦法,但願官方和社區繼續完善。
本文同步發表在github https://github.com/Jon-Millen...