Angular : 在 GET 后使用数据进行路由
Angular : Routing with data after GET
我目前有一个 Angular 2 前端 + Java 后端应用程序。
有一种形式是由一些搜索条件组成的。
提交后,我继续进行搜索:一个返回结果(复杂类型)的 http get 方法。
我想使用路由在另一个页面上显示此结果。
我该如何处理?
我可以在路由器的导航方法中传递一些数据吗?
如有任何帮助,我们将不胜感激。我会继续寻找。
正确的方法是使用服务在组件之间进行通信。
请查看 angular 中的此 link 以查看操作方法。
https://angular.io/docs/ts/latest/cookbook/component-communication.html
您还可以将参数传递给 link 并在路由调用的组件中读取该参数。
希望对您有所帮助。
所以这里的基本思想是你想要子,它是一个表单并基于查询想要在另一个组件中显示结果。
我的建议是使用 routerLink 和参数导航到另一个组件,就像您在表单和第二个组件中输入的内容一样 ngAfterViewInit
您可以查询后端,以便在第二个组件中获得结果.
希望这对您有所帮助。
Angular路由不是为了传递大对象。可以传路由参数和查询参数,不能传复杂对象
如果需要跨组件共享数据,建议使用服务的共享实例来实现。
建议对共享服务使用 observable,但无论如何都不是必需的。示例如下所示:
@Injectable()
export class MySharedService {
// BehaviorSubjects start with a default value and replay the latest value to components that subscribe at any point in the lifecycle.
// Subject types do not start with a default value and do not replay the latest value, so if a component subscribes after it's fired, it won't receive the value.
private someStringDataSource = new BehaviorSubject<string>('');
someString$ = this.someStringDataSource.asObservable();
setSomeString(newString: string) {
this.someStringDataSource.next(newString);
}
}
export class AppComponent implements OnInit, OnDestroy {
sub: Subscription;
constructor(private sharedService: MySharedService) { }
ngOnInit() {
// subscribe to data from service
this.sharedService.someString$.subscribe(data => //do something with data);
//send data to service
this.sharedService.setSomeString('newString');
}
ngOnDestroy(){
this.sub.unsubscribe();
}
}
我目前有一个 Angular 2 前端 + Java 后端应用程序。
有一种形式是由一些搜索条件组成的。 提交后,我继续进行搜索:一个返回结果(复杂类型)的 http get 方法。 我想使用路由在另一个页面上显示此结果。
我该如何处理?
我可以在路由器的导航方法中传递一些数据吗?
如有任何帮助,我们将不胜感激。我会继续寻找。
正确的方法是使用服务在组件之间进行通信。
请查看 angular 中的此 link 以查看操作方法。 https://angular.io/docs/ts/latest/cookbook/component-communication.html
您还可以将参数传递给 link 并在路由调用的组件中读取该参数。
希望对您有所帮助。
所以这里的基本思想是你想要子,它是一个表单并基于查询想要在另一个组件中显示结果。
我的建议是使用 routerLink 和参数导航到另一个组件,就像您在表单和第二个组件中输入的内容一样 ngAfterViewInit
您可以查询后端,以便在第二个组件中获得结果.
希望这对您有所帮助。
Angular路由不是为了传递大对象。可以传路由参数和查询参数,不能传复杂对象
如果需要跨组件共享数据,建议使用服务的共享实例来实现。
建议对共享服务使用 observable,但无论如何都不是必需的。示例如下所示:
@Injectable()
export class MySharedService {
// BehaviorSubjects start with a default value and replay the latest value to components that subscribe at any point in the lifecycle.
// Subject types do not start with a default value and do not replay the latest value, so if a component subscribes after it's fired, it won't receive the value.
private someStringDataSource = new BehaviorSubject<string>('');
someString$ = this.someStringDataSource.asObservable();
setSomeString(newString: string) {
this.someStringDataSource.next(newString);
}
}
export class AppComponent implements OnInit, OnDestroy {
sub: Subscription;
constructor(private sharedService: MySharedService) { }
ngOnInit() {
// subscribe to data from service
this.sharedService.someString$.subscribe(data => //do something with data);
//send data to service
this.sharedService.setSomeString('newString');
}
ngOnDestroy(){
this.sub.unsubscribe();
}
}