【发布时间】:2017-07-22 21:39:05
【问题描述】:
我正在使用 angular2 开发一个应用程序。我有一个场景,我需要在路由(使用 router.navigate())时将复杂数据(对象数组)从一个组件传递到另一个组件(它们不是父子,它们是两个独立的组件)。我用谷歌搜索了这个,大部分结果都描述了父子组件的场景。我浏览了结果,找到了这些传递数据的方法。
1) 创建共享服务 2) 作为路由参数传递
第二种方法对我有用(尽管我不喜欢上面解释的复杂数据)。我无法使用共享服务共享数据。 所以我的问题是,使用服务在组件之间传递数据是否仅在组件处于父子关系时才有效?另外,请告诉我是否还有其他首选方式在一个组件和另一个组件之间传递数据?
更新: 我正在从我的场景中添加一些代码。请让我知道我的错误,为什么通过共享服务传递数据不起作用。
我有 2 个组件:1) SearchComponent 2) TransferComponent 我在SearchComponent中设置数据,想通过utilityService访问TransferComponent中的数据。
实用服务:
import {Injectable} from "@angular/core";
@Injectable()
export class UtilityService{
constructor(){
}
public bioReagentObject = [];
routeBioReagentObj(bioReagentObj){
console.log("From UtilityService...", bioReagentObj);
for (let each of bioReagentObj)
this.bioReagentObject.push(each)
// console.log("From UtilityService",this.bioReagentObject);
}
returnObject(){
console.log("From UtilityService",this.bioReagentObject);
return this.bioReagentObject
}
}
搜索组件.ts
import {UtilityService} from "../services/utilityservice.component";
import {Component, OnInit, OnDestroy, Input} from '@angular/core';
@Component({
selector: 'bioshoppe-search-details',
providers: [UtilityService],
templateUrl: 'app/search/searchdetails.component.html',
styleUrls: ['../../css/style.css']
})
export class SearchDetailsComponent implements OnInit, OnDestroy {
constructor(private route: ActivatedRoute,
private utilityService: UtilityService,
private router: Router) {
}
@Input() selected: Array<String> = [{barcode:123, description:"xyz"}];
//This method is called from .html and this.selected has the data to be shared.
toTransfer() {
this.utilityService.routeBioReagentObj(this.selected);
this.router.navigate(['/transfer']);
}
}
TransferService.ts
import {Component, Input, OnInit, OnDestroy} from '@angular/core';
import {TransferService} from "../services/transferservice.component";
import {UserService} from "../services/userservice.component";
import {SearchService} from "../services/searchservice.component";
import {ActivatedRoute} from '@angular/router';
import {UtilityService} from "../services/utilityservice.component";
@Component({
selector: 'bioshoppe-transfer',
providers: [TransferService, UserService, SearchService, UtilityService],
templateUrl: 'app/transfer/transfer.component.html',
styleUrls: ['../../css/style.css', '../../css/transfer.component.css']
})
export class TransferComponent implements OnInit, OnDestroy{
constructor(private transferService: TransferService,
private userService: UserService,
private searchService: SearchService,
private utilityService: UtilityService,
private route: ActivatedRoute
) {
}
ngOnInit() {
// I am trying to access the data here, but it print "undefind"
console.log("From Transferpage",this.utilityService.returnObject());
}
}
我确定有问题,但只是我无法弄清楚。感谢任何帮助。
【问题讨论】:
-
不,服务不需要有父子关系。服务可以创建为模块的单例,并通过模块重用。
-
一个共享服务可以在任何组件之间共享,它们不必是父/子关系。在大多数情况下,共享服务应该可以工作,您能否提供一些示例代码来说明您拥有什么以及您正在尝试做什么。这样人们就可以尝试给你例子
-
如果你愿意,你可以只拥有 DataService。 stackoverflow.com/questions/36835123/…
-
@davidejones 我添加了代码以使我的场景更加清晰。如果您发现任何问题,请告诉我。
-
如果您想共享一项服务并在其上保存数据,请确保该服务是一个模块的提供者,该模块位于所有其他模块之上。例如,将您的
UtilityService添加到您的app.module.ts的providers:。如果您将提供程序添加到@Component(),您将获得该服务的全新实例,在这种情况下,您的UtilityService。
标签: angular angular2-routing angular2-components