[英]How can I maintain a variable's value between component instances in Angular 9 with caching?
I am trying to create a multi-select component that can be reused throughout my site but which remembers the last selection made so that the user doesn't have to keep making the same selection on different pages.我正在尝试创建一个多选组件,该组件可以在我的整个站点中重复使用,但它会记住上次所做的选择,以便用户不必在不同的页面上继续进行相同的选择。
The following stackblitz demonstrates a simplified version of what I would like to achieve: https://stackblitz.com/edit/angular-y97cbz-yarva4以下 stackblitz 演示了我想要实现的简化版本: https ://stackblitz.com/edit/angular-y97cbz-yarva4
In the above example I would like the component to remember the selection made on Page One and select these choices by default on the component instance on Page Two.在上面的示例中,我希望组件记住在第一页上所做的选择,并在第二页上的组件实例上默认选择这些选项。
I've tried to use a service to store the selection which works when I disable my page caching but breaks as soon as I turn the caching on!我尝试使用服务来存储选择,该服务在禁用页面缓存时有效,但一旦打开缓存就中断! Ideally I need the pages to be cached, though, due to the large amount of data they contain in the real application.
理想情况下,我需要缓存页面,因为它们包含在实际应用程序中的大量数据。
Is there a way to achieve this without disabling the caching?有没有办法在不禁用缓存的情况下实现这一目标?
Try to build a new component that will hold your selection in their service and use that component in other templates via selector
.尝试构建一个新组件,将您的选择保存在他们的服务中,并通过
selector
在其他模板中使用该组件。 That is exactly what is in stackblitz that you provide in your question.这正是您在问题中提供的 stackblitz 中的内容。
If you can please provide some of your code, which will be more useful for us to help you.如果可以,请提供您的一些代码,这对我们帮助您更有用。 I hope that you'll make it work.
我希望你能成功。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.