简体   繁体   English

Angular4存储NodeJS数据

[英]Angular4 store NodeJS data

I just got started working with the Angular4 and NodeJS. 我刚开始使用Angular4和NodeJS。

I have a signin post request that sends data (eg user role, first name, last name) to my angular app and stores it in a variable. 我有一个登录后请求,该请求将数据(例如,用户角色,名字,姓氏)发送到我的角度应用程序并将其存储在变量中。 However, when I reload the page the variables are empty again. 但是,当我重新加载页面时,变量再次为空。

So I was wondering, is there a way of avoiding this (eg fetching the necessary data on every page reload?). 所以我想知道,有没有一种方法可以避免这种情况(例如在每次重新加载页面时获取必要的数据?)。 Or is there a better practice to store the data that you receive from a post request in angular? 还是有更好的做法将您从发帖请求中收到的数据存储在angular中? Localstorage and sessionstorage dont seem really save to me. 本地存储和sessionstorage似乎并没有真正拯救我。

I tried to Google this for a couple of hours, but I couldn't find a suitable answer. 我尝试使用Google进行了几个小时的搜索,但找不到合适的答案。

If you are just navigating from one route to other using 如果您只是使用以下方法从一条路线导航到另一条路线
this.router.navigate(['/heroes']);

then you can save your data in a service variable or pass it between the routes using this.router.navigate(['/heroes', { firstName: 'First Name', lastName: 'Last Name' }]); 然后您可以将数据保存在服务变量中,或使用this.router.navigate(['/heroes', { firstName: 'First Name', lastName: 'Last Name' }]);在路由之间传递数据this.router.navigate(['/heroes', { firstName: 'First Name', lastName: 'Last Name' }]);

Instead if you plan to explicitly reload the page then angular has no control over it as http is stateless protocol.You can consider using LocalStorage or Cookies to save your data if required. 相反,如果您打算显式重新加载页面,则由于http是无状态协议,所以angular无法对其进行控制。如果需要,可以考虑使用LocalStorage或Cookies来保存数据。

Alternatively check Angular Resolve to load the data before initing your component, so that you have all the data available beforehand. 或者,在初始化组件之前选中Angular Resolve以加载数据,以便事先获得所有可用数据。

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

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