angular 2 在重定向后保留来自 URL 的状态 [英] angular 2 retain state from URL after redirect
问题描述
我在 /app/123
处启动我的 Angular 应用程序的 index.html
文件,并希望使用 /app
重定向到处理 /app
的控制器code>123 保留有关最初查询内容的信息.
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.
如何实现?
推荐答案
您有多种选择:
1) 您可以使用查询参数保留有关路线的信息.这允许您在组件之间来回传递相同的参数.
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) 或者,您可以构建一个服务来保存您的共享数据.服务是为您的应用程序保留数据的好方法.(但请不要使用 BehaviorSubject.在大多数情况下完全没有必要.)
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/
基本服务如下所示:
import { Injectable } from '@angular/core';
@Injectable()
export class DataService {
serviceData: string;
}
还有一个 plunker:https://plnkr.co/edit/KT4JLmpcwGBM2xdZQeI9?p=预览
And a plunker here: https://plnkr.co/edit/KT4JLmpcwGBM2xdZQeI9?p=preview
这篇关于angular 2 在重定向后保留来自 URL 的状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!