LocationStrategy.pushState(state: any, title: string, url: string, queryParams: string) - 什么是状态?

LocationStrategy.pushState(state: any, title: string, url: string, queryParams: string) - What is state?

我可以用 pushState(state: any, title: string, url: string, queryParams: string) 中的状态参数做什么?

我可以存储数据吗?

有没有比 https://angular.io/api/common/LocationStrategy#pushState 多 'detailed' 的文档?

<3

pushState 功能最终将以某种方式映射到浏览器的 history.pushState 功能。 MDN docs 详细描述了如何使用此 state 属性:

The state object is a JavaScript object which is associated with the new history entry created by pushState(). Whenever the user navigates to the new state, a popstate event is fired, and the state property of the event contains a copy of the history entry's state object.

The state object can be anything that can be serialized. Because Firefox saves state objects to the user's disk so they can be restored after the user restarts the browser, we impose a size limit of 640k characters on the serialized representation of a state object. If you pass a state object whose serialized representation is larger than this to pushState(), the method will throw an exception. If you need more space than this, you're encouraged to use sessionStorage and/or localStorage.

(强调 mine/OP 的)。

这里描述的 popstate 事件在 Angular 中可以通过它的 Location service. Specifically, you can subscribe to the Location service in order to listen for PopStateEvent 获得,其中包括一个 state 属性 包含你所期望的。