Giter VIP home page Giter VIP logo

Comments (8)

switer avatar switer commented on July 29, 2024 2

中间利用的是小程序页面实例化的时间差,不管怎样,请求都是被提前发起了,必然会起到优化效果

from wxpage.

switer avatar switer commented on July 29, 2024

一般预加载是使用提前生成Promise实例执行加载流程,只要在Promise的回调后就不需要关心异步逻辑是进行中 or 完成

from wxpage.

yudekai avatar yudekai commented on July 29, 2024

一般预加载是使用提前生成Promise实例执行加载流程,只要在Promise的回调后就不需要关心异步逻辑是进行中 or 完成

稍微有点不明白,我需要在哪里执行Promise?第一个页面还是第二个页面?有相关的示例代码吗?

from wxpage.

switer avatar switer commented on July 29, 2024
// 页面index
Page.P('index', {
   clickToDetail() {
      this.$preload('detail')
   }
})
// 页面detail
Page.P('detail', {
   onPreload() {
      this.$put('detail_preload_fetch', this.fetch())
   },
   onLoad() {
     ;(this.$take('detail_preload_fetch')||this.fetch()).then(data=>{
        console.log(data)
        // do somthing
     })
   },
   fetch() {
      return fetch('xxx')
   }
})

from wxpage.

yudekai avatar yudekai commented on July 29, 2024

嗯嗯,终于明白了,谢谢大侠!
但是这样的话,其实还是存在一个问题,就是还是有可能this.$take('detail_preload_fetch')暂时还没有数据,那这样的话就执行的是this.fetch()的返回数据。如果是这样,那么本次跳转其实就没有达到预加载的效果,对吧?当然这个要看网络情况,如果网络良好,多半跳转还是能够在onLoad时拿到缓存数据的。不知道我理解的有问题吗?

from wxpage.

yudekai avatar yudekai commented on July 29, 2024

中间利用的是小程序页面实例化的时间差,不管怎样,请求都是被提前发起了,必然会起到优化效果

这样理解也对,嗯。感谢。

from wxpage.

ice0526 avatar ice0526 commented on July 29, 2024

文档不是讲$preload的数据不一定会被用到 这种预加载的数据一般会保存在storage内,因为用户不一定会马上访问页面,而把数据存在全局变量会增加小程序占用的内存。微信会毫不犹豫的把内存占用过大的小程序给杀掉 如果这样的话 put take 就不适用了吧

from wxpage.

Mister-Hope avatar Mister-Hope commented on July 29, 2024

文档不是讲$preload的数据不一定会被用到 这种预加载的数据一般会保存在storage内,因为用户不一定会马上访问页面,而把数据存在全局变量会增加小程序占用的内存。微信会毫不犹豫的把内存占用过大的小程序给杀掉 如果这样的话 put take 就不适用了吧

这就需要你的判断了,我的页面大多是消息页用户需要停留一段时间,这个时间我就会把页面可以触及的链接给预加载了,而在当前页面的unload中我又注册了一个清除所有子页面预加载数据的逻辑。这样的话能有效减少页面的内存占用。

from wxpage.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.