我正在做一个 Angular 项目。我需要使用 router.navigate 将一个数组从一个组件传递到另一个组件,但无法做到

I am working on an Angular project. I need to pass an array from one component to another using router.navigate but unable to do it

我正在研究 Angular 6. 我正在尝试使用 router.navigate method.In 组件 BBB 将数组从组件 AAA 传递到组件 BBB 我正在尝试使用快照,但无法获取阵列。

组件 AAA

export class AAA implements OnInit {
private testCases: TestCase[] = [];

constructor(private router: Router) { }

ngOnInit() {
}

onSubmit(form: NgForm) {
const value = form.value;
console.log("form values: "+value.testid);
const newTestCase = new TestCase(value.testid, value.testPriority, 
value.testSummary);
console.log(newTestCase);
this.testCases.push(newTestCase);
console.log(this.testCases);
this.navToTable(this.testCases);
}

navToTable(testCase: TestCase[]) {
this.router.navigate(['table', {testCase}]);
}
}    

分量 BBB

export class BBB {
constructor(private route: ActivatedRoute) { 
this.testCase = this.route.snapshot.params['testCase'];
console.log(this.testCase);  
}

printing output as [object object]

两个路由组件之间的通信通常意味着您通过 URL 传递 ID 或字符串。

传递整个数组是一种不好的做法,更不用说很糟糕了。

相反,请考虑将 Components interactions (hint : you can also use a resolver 与服务相结合,在路由方面提供更多功能)

如果你真的需要在两条路由之间交换大量数据,你应该为此创建一个专用服务,它就像一个单例包装一个堆栈,你可以在其中推送和弹出这些数据。当然,您也可以做一个包含地图的服务,您可以在其中获取特定组件的数据。

然而,这可能仍然是不好的做法,因为它不常见,难以调试并且 error-prone。

解析器也可能有效,但如果由于任何原因无法传递 ID,我会使用服务解决方案。