简体   繁体   English

即使未导航到Angular 2,Angular 2也会使路由保持活动状态

[英]Angular 2 keep route alive even when not navigated to it

In angular 2 is there a way to keep a navigation route alive even when it isn't navigated to? 在角度2中是否有一种方法可以保持导航路线的活动,即使它没有导航到? My purpose for this is I'm attempting to put a webgl canvas into a angular 2 route. 我的目的是为了尝试将webgl画布放入有角度的2路。 Now by default whenever a route is navigated to it initializes the component and loads all of the needed resources then when navigating away it destroys the component and removes the view from the Dom. 现在默认情况下,只要路由导航到它,就会初始化组件并加载所有需要的资源,然后在导航时它会破坏组件并从Dom中删除视图。 This causes 2 problems when using webgl the main one being there are only a specific amount of instances of webgl allowed otherwise it could overload the graphics card. 当使用webgl时,这会导致2个问题,主要的一个是只允许特定数量的webgl实例,否则会导致显卡过载。 So if the route is navigated to and away from multiple times there's a chance that limit can be reached because it's creating a new webgl instance every time the route is navigated to. 因此,如果路由被多次导航和远离,则有可能达到限制,因为每次导航到路径时它都会创建一个新的webgl实例。 Also webgl often time requires complex variables and resources and it would be nice to be able to keep those variables alive so they don't have to be loaded again and in some cases even use them outside of the route. 此外,webgl通常需要复杂的变量和资源,能够保持这些变量的存活会很好,因此不必再次加载它们,在某些情况下甚至可以在路径之外使用它们。

So my proposed solution is to keep the component alive but hidden so that resources and variables are kept alive is there any way to do this? 所以我提出的解决方案是保持组件存活但隐藏,以便资源和变量保持活动有没有办法做到这一点?

I think that you could try to use the CanReuse interface and its routerCanReuse . 我认为您可以尝试使用CanReuse接口及其routerCanReuse If the component implements this method and it returns true, the component won't be destroyed and the same component instance will be reused across routes: 如果组件实现此方法并返回true,则不会销毁该组件,并且将跨路由重用相同的组件实例:

@Component({
  selector: 'my-cmp',
  template: `
    (...)
  `
})
class MyCmp implements CanReuse, OnReuse {
  routerCanReuse(next: ComponentInstruction, prev: ComponentInstruction) {
    return true;
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM