Keep-alive on a single route instaead of all routes in router-view

余生颓废 提交于 2019-12-03 15:54:29

问题


if keep-alive is specified to router-view as below

<keep-alive>
    <router-view></router-view>
</keep-alive>

then all routes are effectively cached and reloaded when that route is revisited.

I'd like to be able to specify a keep-alive option on individual routes.

With many routes and only 1 or 2 that need to be kept alive wihout re-rendering caching all routes is useless

is there any method of doing so or any workaround available


回答1:


https://jsfiddle.net/Linusborg/L613xva0/4/

New in Vue version 2.1.0, the include and exclude props for conditionally caching components. Note the use of the name option.

const Foo = {
    name: 'foo',
  template: '<div><p v-for="n in numbers">{{ n }}</p></div>',
  data: function() {
    return {
        numbers: [Math.round(Math.random() * 10), Math.round(Math.random() * 10)]
    }
  }
}

const Bar = {
    name: 'bar',
    template: '<div><p v-for="n in numbers"><strong>{{ n }}</strong></p></div>',
  data: function() {
    return {
        numbers: [Math.round(Math.random() * 10), Math.round(Math.random() * 10)]
    }
  }
}


来源:https://stackoverflow.com/questions/43116616/keep-alive-on-a-single-route-instaead-of-all-routes-in-router-view

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!