Итак, у меня есть 2 разных компонента и 1 компонент шаблона. Я хотел бы передать компонент шаблона в 2 других компонента (я просто положил в html выход маршрутизатора два раза). Но .. 2 компонент имеет переменную uniq. как это: component_type = "region", component_type = "request". Я пытаюсь обмениваться данными между службой, и она тоже работает, но общие данные всегда одинаковы. Поэтому я хотел бы связать свойство uniq двух компонентов в компоненте шаблона. Проблема на этом рисунке.
![enter image description here](https://i.stack.imgur.com/cWvsP.png)
Итак, я хочу, чтобы первое значение было первым идентификатором uniq, второе - вторым идентификатором uniq.
Спасибо. Мой service.ts:
@Injectable()
export class ObjectTemplateService {
selectedRequest: string;
private objectType$: BehaviorSubject<string> = new BehaviorSubject<string>('Default ntype');
constructor(private http: Http,
) { }
public setRequestTypeProperty(newrequestType: string): void {
this.objectType$.next(newrequestType);
}
public getObjectType(): Observable<string> {
return this.objectType$.asObservable();
}
}
Мой шаблон component.ts:
@Component({
selector: 'app-object-template',
templateUrl: './object-template.component.html',
styleUrls: ['./object-template.component.scss']
})
export class ObjectTemplateComponent implements OnInit {
objectdata = REQUEST_DATA
selectedRequest: string;
selectedReq: RequestModel;
public objectType: string;
private getobjectTypeSubscription: Subscription;
constructor(public objectTemplateService: ObjectTemplateService) { }
ngOnInit() {
this.getobjectTypeSubscription = this.objectTemplateService.getObjectType()
.subscribe((objectType: string) => {
console.log('Object Template get these =>' + objectType)
return this.objectType = objectType;
});
}
onSelect(req: RequestModel): void {
this.selectedReq = req;
}
getRequestType(request: RequestModel): void {
this.objectTemplateService.getRequestType(request);
this.selectedRequest = request.object_id
this.objectTemplateService.changeMessage(request.object_id.toString())
this.onSelect(request)
}
}
Мой шаблонный компонент. html
Type: {{objectType}}
Первый компонент.ts:
@Component({
selector: 'app-company',
templateUrl: './company.component.html',
styleUrls: ['./company.component.css']
})
export class CompanyComponent implements OnInit {
public object_type: string = 'company';
constructor(public objectTemplateService: ObjectTemplateService) { }
ngOnInit() {
this.objectTemplateService.setRequestTypeProperty(this.object_type);
}
}
Первый компонент. html
<app-object-template></app-object-template>
Второй компонент.ts
@Component({
selector: 'app-requests',
templateUrl: './requests.component.html',
styleUrls: ['./requests.component.css']
})
export class RequestsComponent implements OnInit {
public object_type: string = 'request';
constructor( public objectTemplateService: ObjectTemplateService) { }
ngOnInit() {
this.objectTemplateService.setRequestTypeProperty(this.object_type);
}
}
Второй компонент. html
<app-object-template></app-object-template>