javascript - 如何在路由器导航中发送数组?

标签 javascript angular typescript angular6 angular2-routing

在其他问题中搜索了解决方案,但没有任何帮助..

我希望重定向到 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/

相关文章:

php - 带有单选按钮的表单提交到 mysql

angular - 未找到模块 : Error: Can't resolve '@angular/http' ?

javascript - 单击图标无法导航到新页面

angular - 单元测试后关闭 ngx-bootstrap 模式

javascript - RxJs:当所有取消订阅时中止延迟和共享的可观察对象

javascript - laravel5删除,如何将2个参数传递给DELETE操作

javascript - jQuery stopPropagation 被忽略

javascript - Leaflet,重现/调用拖动事件

javascript - 如何在 Typescript 中导出具有其他名称的命名空间

javascript - 我怎样才能获得点击的链接href?