Angular 4 - 将对象从一个组件传递到另一个组件(无父子层次结构)
Angular 4 - Pass an object from one component to another (no parent - child hierarchy)
我的情况:
我有一个显示图块的组件,每个图块代表一个数组中的一个对象,该数组用 ngfor 循环。
单击图块时,我想将该对象传递给另一个组件,该组件负责在可以修改的字段中显示该对象的所有属性。
我试过的:
在做了一些研究并看到多个帖子向我展示如何实现父子层次结构以及一些帖子解释说有必要使用共享服务以实现所需的功能之后,我决定尝试设置这样的服务。
但是,我似乎不明白什么时候应该导航到不同的路线。似乎导航很早就找到了位置,因为在详细信息组件中检索传递给服务的对象是未定义的。
我的代码:
显示图块的组件具有以下功能,可将单击的对象传递给共享服务:
editPropertyDetails(property: Property) {
console.log('Edit property called');
return new Promise(resolve => {
this.sharedPropertyService.setPropertyToDisplay(property);
resolve();
}).then(
() => this.router.navigate(['/properties/detail'])
)
}
共享服务有一个设置 属性 对象的函数和一个检索对象的函数,如下所示:
@Injectable()
export class SharedPropertyService {
// Observable
public propertyToDisplay = new Subject<Property>();
constructor( private router: Router) {}
setPropertyToDisplay(property: Property) {
console.log('setPropertyToDisplay called');
this.propertyToDisplay.next(property);
}
getPropertyToDisplay(): Observable<Property> {
console.log('getPropertyToDisplay called');
return this.propertyToDisplay.asObservable();
}
}
最后是细节组件,它必须接收被点击的对象但得到一个未定义的对象:
export class PropertyDetailComponent implements OnDestroy {
property: Property;
subscription: Subscription;
constructor(private sharedPropertyService: SharedPropertyService) {
this.subscription = this.sharedPropertyService.getPropertyToDisplay()
.subscribe(
property => { this.property = property; console.log('Detail Component: ' + property.description);}
);
}
ngOnDestroy() {
// When view destroyed, clear the subscription to prevent memory leaks
this.subscription.unsubscribe();
}
}
提前致谢!
请尝试以下示例:
第 1 步:创建服务 [DataService]
import { Injectable } from '@angular/core';
import { BehaviorSubject } from 'rxjs/BehaviorSubject';
@Injectable()
export class DataService {
private userIdSource = new BehaviorSubject<number>(0);
currentUser = this.userIdSource.asObservable();
private orderNumSource = new BehaviorSubject<number>(0);
currentOrder = this.orderNumSource.asObservable();
constructor() { }
setUser(userid: number) {
this.userIdSource.next(userid)
}
setOrderNumber(userid: number) {
this.orderNumSource.next(userid)
}
}
第二步:在登录组件中设置值
import { Component } from '@angular/core';
import { DataService } from "../services/data.service";
@Component({
selector: 'app-login',
templateUrl: './login.component.html',
styleUrls: ['./login.component.css']
})
export class LoginComponent {
constructor( private dataService:DataService) { }
onSubmit() {
this.dataService.setUser(1);
}
}
第 3 步:在另一个组件中获取值
import { Component, OnInit } from '@angular/core';
import { DataService } from "../services/data.service";
@Component({
selector: 'app-shopping-cart',
templateUrl: './shopping-cart.component.html',
styleUrls: ['./shopping-cart.component.css']
})
export class ShoppingCartComponent implements OnInit {
userId: number = 0;
constructor(private dataService: DataService) { }
ngOnInit() {
this.getUser();
}
getUser() {
this.dataService.currentUser.subscribe(user => {
this.userId = user
}, err => {
console.log(err);
});
}
}
注意:页面刷新时该值会丢失。
像这样尝试:
尝试订阅 this.sharedPropertyService.propertyToDisplay
而不是 this.sharedPropertyService.getPropertyToDisplay()
this.sharedPropertyService.propertyToDisplay.subscribe((property) => {
this.property = property;
console.log('Detail Component: ' + property.description);
});
并发送如下对象:
editPropertyDetails(property: Property) {
this.sharedPropertyService.setPropertyToDisplay(property);
}
你的控制台输出什么? this.property
是否曾在子组件上设置过?
我会尝试摆脱这个功能:
getPropertyToDisplay(): Observable<Property>
并尝试直接访问 propertyToDisplay
。
此外,.navigate 可以将数据作为第二个参数,因此您可以尝试在路由更改中传递数据。
constructor(
private route: ActivatedRoute,
private router: Router) {}
ngOnInit() {
this.property = this.route
.variableYouPassedIntoNavigator
我解决了这个问题,方法是传递被点击的瓦片对象的 id,就像在路线的导航附加部分中一样,然后使用细节组件中的服务根据传递的 id 获取对象路线.
我将提供下面的代码,希望没有人需要再次经历所有这些。
显示图块的组件,单击这些图块可以查看其中包含的对象的详细信息:
editPropertyDetails(property: Property) {
console.log('Edit property called');
let navigationExtras: NavigationExtras = {
queryParams: {
"property_id": property.id
}
};
this.router.navigate(['/properties/detail'], navigationExtras);
}
接收被点击对象的详情组件
private sub: any;
propertyToDisplay: Property;
constructor
(
private sharedPropertyService: SharedPropertyService,
private router: Router,
private route: ActivatedRoute
) {}
ngOnInit() {
this.sub = this.route.queryParams.subscribe(params => {
let id = params["property_id"];
if(id) {
this.getPropertyToDisplay(id);
}
});
}
getPropertyToDisplay(id: number) {
this.sharedPropertyService.getPropertyToDisplay(id).subscribe(
property => {
this.propertyToDisplay = property;
},
error => console.log('Something went wrong'));
}
// Prevent memory leaks
ngOnDestroy() {
this.sub.unsubscribe();
}
服务
properties: Property[];
constructor( private propertyService: PropertyService) {}
public getPropertyToDisplay(id: number): Observable<Property> {
if (this.properties) {
return this.findPropertyObservable(id);
} else {
return Observable.create((observer: Observer<Property>) => {
this.getProperties().subscribe((properties: Property[]) => {
this.properties = properties;
const prop = this.filterProperties(id);
observer.next(prop);
observer.complete();
})
}).catch(this.handleError);
}
}
private findPropertyObservable(id: number): Observable<Property> {
return this.createObservable(this.filterProperties(id));
}
private filterProperties(id: number): Property {
const props = this.properties.filter((prop) => prop.id == id);
return (props.length) ? props[0] : null;
}
private createObservable(data: any): Observable<any> {
return Observable.create((observer: Observer<any>) => {
observer.next(data);
observer.complete();
});
}
private handleError(error: any) {
console.error(error);
return Observable.throw(error.json().error || 'Server error');
}
private getProperties(): Observable<Property[]> {
if (!this.properties) {
return this.propertyService.getProperties().map((res: Property[]) => {
this.properties = res;
console.log('The properties: ' + JSON.stringify(this.properties));
return this.properties;
})
.catch(this.handleError);
} else {
return this.createObservable(this.properties);
}
}
我正在研究类似的功能并遇到了同样的问题(未定义)。你可以这样初始化。
public propertyToDisplay = new BehaviorSubject<Property>(undefined);
像这样进行更改后。我能够从服务文件中的 Observable
以及我尝试使用此服务的组件中获取值。
正在使用服务
首先,您在服务上创建一个功能。
调用该函数并使用其他组件。
写这段代码
this.handleReq.handlefilterdata.subscribe(() => {
this.ngDoCheck();
});
在这里,
handleReq 是服务。
handlefilterdata 是 rxjs 主题。
我的情况:
我有一个显示图块的组件,每个图块代表一个数组中的一个对象,该数组用 ngfor 循环。 单击图块时,我想将该对象传递给另一个组件,该组件负责在可以修改的字段中显示该对象的所有属性。
我试过的:
在做了一些研究并看到多个帖子向我展示如何实现父子层次结构以及一些帖子解释说有必要使用共享服务以实现所需的功能之后,我决定尝试设置这样的服务。
但是,我似乎不明白什么时候应该导航到不同的路线。似乎导航很早就找到了位置,因为在详细信息组件中检索传递给服务的对象是未定义的。
我的代码:
显示图块的组件具有以下功能,可将单击的对象传递给共享服务:
editPropertyDetails(property: Property) {
console.log('Edit property called');
return new Promise(resolve => {
this.sharedPropertyService.setPropertyToDisplay(property);
resolve();
}).then(
() => this.router.navigate(['/properties/detail'])
)
}
共享服务有一个设置 属性 对象的函数和一个检索对象的函数,如下所示:
@Injectable()
export class SharedPropertyService {
// Observable
public propertyToDisplay = new Subject<Property>();
constructor( private router: Router) {}
setPropertyToDisplay(property: Property) {
console.log('setPropertyToDisplay called');
this.propertyToDisplay.next(property);
}
getPropertyToDisplay(): Observable<Property> {
console.log('getPropertyToDisplay called');
return this.propertyToDisplay.asObservable();
}
}
最后是细节组件,它必须接收被点击的对象但得到一个未定义的对象:
export class PropertyDetailComponent implements OnDestroy {
property: Property;
subscription: Subscription;
constructor(private sharedPropertyService: SharedPropertyService) {
this.subscription = this.sharedPropertyService.getPropertyToDisplay()
.subscribe(
property => { this.property = property; console.log('Detail Component: ' + property.description);}
);
}
ngOnDestroy() {
// When view destroyed, clear the subscription to prevent memory leaks
this.subscription.unsubscribe();
}
}
提前致谢!
请尝试以下示例:
第 1 步:创建服务 [DataService]
import { Injectable } from '@angular/core';
import { BehaviorSubject } from 'rxjs/BehaviorSubject';
@Injectable()
export class DataService {
private userIdSource = new BehaviorSubject<number>(0);
currentUser = this.userIdSource.asObservable();
private orderNumSource = new BehaviorSubject<number>(0);
currentOrder = this.orderNumSource.asObservable();
constructor() { }
setUser(userid: number) {
this.userIdSource.next(userid)
}
setOrderNumber(userid: number) {
this.orderNumSource.next(userid)
}
}
第二步:在登录组件中设置值
import { Component } from '@angular/core';
import { DataService } from "../services/data.service";
@Component({
selector: 'app-login',
templateUrl: './login.component.html',
styleUrls: ['./login.component.css']
})
export class LoginComponent {
constructor( private dataService:DataService) { }
onSubmit() {
this.dataService.setUser(1);
}
}
第 3 步:在另一个组件中获取值
import { Component, OnInit } from '@angular/core';
import { DataService } from "../services/data.service";
@Component({
selector: 'app-shopping-cart',
templateUrl: './shopping-cart.component.html',
styleUrls: ['./shopping-cart.component.css']
})
export class ShoppingCartComponent implements OnInit {
userId: number = 0;
constructor(private dataService: DataService) { }
ngOnInit() {
this.getUser();
}
getUser() {
this.dataService.currentUser.subscribe(user => {
this.userId = user
}, err => {
console.log(err);
});
}
}
注意:页面刷新时该值会丢失。
像这样尝试:
尝试订阅 this.sharedPropertyService.propertyToDisplay
而不是 this.sharedPropertyService.getPropertyToDisplay()
this.sharedPropertyService.propertyToDisplay.subscribe((property) => {
this.property = property;
console.log('Detail Component: ' + property.description);
});
并发送如下对象:
editPropertyDetails(property: Property) {
this.sharedPropertyService.setPropertyToDisplay(property);
}
你的控制台输出什么? this.property
是否曾在子组件上设置过?
我会尝试摆脱这个功能:
getPropertyToDisplay(): Observable<Property>
并尝试直接访问 propertyToDisplay
。
此外,.navigate 可以将数据作为第二个参数,因此您可以尝试在路由更改中传递数据。
constructor(
private route: ActivatedRoute,
private router: Router) {}
ngOnInit() {
this.property = this.route
.variableYouPassedIntoNavigator
我解决了这个问题,方法是传递被点击的瓦片对象的 id,就像在路线的导航附加部分中一样,然后使用细节组件中的服务根据传递的 id 获取对象路线.
我将提供下面的代码,希望没有人需要再次经历所有这些。
显示图块的组件,单击这些图块可以查看其中包含的对象的详细信息:
editPropertyDetails(property: Property) {
console.log('Edit property called');
let navigationExtras: NavigationExtras = {
queryParams: {
"property_id": property.id
}
};
this.router.navigate(['/properties/detail'], navigationExtras);
}
接收被点击对象的详情组件
private sub: any;
propertyToDisplay: Property;
constructor
(
private sharedPropertyService: SharedPropertyService,
private router: Router,
private route: ActivatedRoute
) {}
ngOnInit() {
this.sub = this.route.queryParams.subscribe(params => {
let id = params["property_id"];
if(id) {
this.getPropertyToDisplay(id);
}
});
}
getPropertyToDisplay(id: number) {
this.sharedPropertyService.getPropertyToDisplay(id).subscribe(
property => {
this.propertyToDisplay = property;
},
error => console.log('Something went wrong'));
}
// Prevent memory leaks
ngOnDestroy() {
this.sub.unsubscribe();
}
服务
properties: Property[];
constructor( private propertyService: PropertyService) {}
public getPropertyToDisplay(id: number): Observable<Property> {
if (this.properties) {
return this.findPropertyObservable(id);
} else {
return Observable.create((observer: Observer<Property>) => {
this.getProperties().subscribe((properties: Property[]) => {
this.properties = properties;
const prop = this.filterProperties(id);
observer.next(prop);
observer.complete();
})
}).catch(this.handleError);
}
}
private findPropertyObservable(id: number): Observable<Property> {
return this.createObservable(this.filterProperties(id));
}
private filterProperties(id: number): Property {
const props = this.properties.filter((prop) => prop.id == id);
return (props.length) ? props[0] : null;
}
private createObservable(data: any): Observable<any> {
return Observable.create((observer: Observer<any>) => {
observer.next(data);
observer.complete();
});
}
private handleError(error: any) {
console.error(error);
return Observable.throw(error.json().error || 'Server error');
}
private getProperties(): Observable<Property[]> {
if (!this.properties) {
return this.propertyService.getProperties().map((res: Property[]) => {
this.properties = res;
console.log('The properties: ' + JSON.stringify(this.properties));
return this.properties;
})
.catch(this.handleError);
} else {
return this.createObservable(this.properties);
}
}
我正在研究类似的功能并遇到了同样的问题(未定义)。你可以这样初始化。
public propertyToDisplay = new BehaviorSubject<Property>(undefined);
像这样进行更改后。我能够从服务文件中的 Observable
以及我尝试使用此服务的组件中获取值。
正在使用服务 首先,您在服务上创建一个功能。 调用该函数并使用其他组件。 写这段代码
this.handleReq.handlefilterdata.subscribe(() => {
this.ngDoCheck();
});
在这里, handleReq 是服务。 handlefilterdata 是 rxjs 主题。