简体   繁体   English

如何使用缓存在 Angular 9 中的组件实例之间维护变量的值?

[英]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.

相关问题 我如何在角度2的2个分量之间共享变量值 - How can i share a variable value between 2 components in angular 2 如何创建角度组件的多个实例? - How can I create multiple instances of an Angular Component? 如何从 Angular 中的一个组件到另一个组件获取任何字符串变量的值 - How can I get the value of any String variable from one component to another component in Angular 如何获取 I18n 变量值我可以返回到我的 Angular 父组件? - How to get a I18n variable value I can return to my Angular parent component? 如何在多个 Angular 2 项目之间共享 Angular 2 组件? - How can I share an Angular 2 component between multiple Angular 2 projects? 如何将Angular Material自动完成中的值分配给组件中的变量? - How can I assign a value from the Angular Material autocomplete to a variable in my component? 角度4:如何在不由父级执行任何额外工作的情况下,通过子级组件中的更改值来更新父级组件的值 - Angular 4: How can I get the Parent Component's value to be updated by a changed value in a child Component without the Parent doing any extra work 如何将值发送到模态并在模态的组件中读取它? Angular 8 - How can I send a value to a modal and read it in modal's component? Angular 8 我如何在Angular 2中的管道和组件之间进行通信 - How can I communicate between Pipe and Component in Angular 2 如何根据 Angular 组件变量的值有条件地设置输入标签的最大值 - How to conditionally set input tag's max value base on a value of a Angular component's variable
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM