0

我目前有一個Angular 2前端+ Java後端應用程序。Angular:獲得GET後的數據路由

有一種形式女巫是由一些搜索條件組成的。 在提交時,我繼續搜索:返回結果(複雜類型)的http get方法。 我想使用路由在另一頁上顯示此結果。

我該如何處理?

我可以在路由器的導航方法中傳遞一些數據嗎?

任何幫助,將不勝感激。我會繼續搜索。

回答

0

所以基本的想法在這裏是你想要的,它是一種形式,並基於查詢想要在另一個組件中顯示結果。

我的建議是使用routerLink和參數導航到另一個組件,無論您在表單中輸入什麼,在第二個組件ngAfterViewInit中,您都可以查詢後端,以便您可以在第二個組件中獲取結果。

希望這有助於。

0

角路由不是爲了傳遞大對象。您可以傳遞路由參數和查詢參數,但不能傳遞複雜的對象。

如果您需要跨組件共享數據,建議使用該服務的共享實例來執行此操作。

建議使用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();  
    } 

}