重定向后,角度2从URL保留状态 [英] angular 2 retain state from URL after redirect

查看:109
本文介绍了重定向后,角度2从URL保留状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从/app/123开始我的角度应用程序的index.html文件,并希望使用123重定向到处理/app的控制器,以保留有关最初查询的内容的信息.

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; 
}

这里有一个小伙子: https://plnkr.co/edit/KT4JLmpcwGBM2xdZQeI9?p=预览

这篇关于重定向后,角度2从URL保留状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆