angular 2 retain state from URL after redirect

半城伤御伤魂 提交于 2019-12-06 16:28:23

问题


I start my angular app's index.html file at /app/123 and want to redirect to the controller handling /app with 123 to retain the information about what was queried initially.

How can this be accomplished?


回答1:


You have several choices:

1) You can retain information on a route using query parameters. That allows you to pass the same parameters back and forth between components.

2) Alternatively, you can build a service to hold onto your shared data. Services are a great way to hold onto data for your application. (But PLEASE don't use BehaviorSubject. It is completely unnecessary in most scenarios.)

I have an example service here:

https://blogs.msmvps.com/deborahk/build-a-simple-angular-service-to-share-data/

The basic service looks like this:

import { Injectable } from '@angular/core';

@Injectable() 
export class DataService {
  serviceData: string; 
}

And a plunker here: https://plnkr.co/edit/KT4JLmpcwGBM2xdZQeI9?p=preview



来源:https://stackoverflow.com/questions/45639474/angular-2-retain-state-from-url-after-redirect

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!