在其他问题中搜索了解决方案,但没有任何帮助..
我希望重定向到 url,例如,
this.router.navigateByUrl('/products');
我需要在其中传递数组并需要在具有事件链接 products
的组件中获取它,使用跳过位置更改而不在 url 中显示任何内容。
数组就像,
products = [{"id":1,"name":"Product One","id":2,"name":"Product Three","id":3,"name":"Product Six"}]
我需要在路由器链接中传递整个数组,并需要使用 skipLocation Change true 在另一个组件(产品)事件链接中检索它..
尝试使用 sharedService
但我在正确的时间点遇到数据加载问题,因此我决定通过路由器链接使用..
如果这不是一个好方法,请建议不使用共享服务的其他替代方法..
最佳答案
您可以将 Angular 服务用于大数据。
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';
import { Subject } from 'rxjs/Subject';
@Injectable()
export class ExampleService {
private subject = new Subject<any>();
updateRouteData(data) {
this.subject.next(data);
}
routeData(): Observable<any> {
return this.subject.asObservable();
}
}
在您的组件中;
用于设置路线数据;
import { ExampleService } from '/example.service'
export class ComponentOne{
constructor(private exampleService:ExampleService){
this.exampleService.updateRouteData(data)
}
您可以像这样传递数据;
import { ExampleService } from '/example.service'
export class ComponentTwo{
constructor(private exampleService:ExampleService){
this.exampleService.routeData().subscribe(data => {
console.log(data)
})
}
关于javascript - 如何在路由器导航中发送数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53494813/