据我所知,有许多方法可以在父子组件和父组件之间进行交互,例如EventEmitter
。但有没有办法在不是父子关系或子关系的组件之间进行交互,这与为此目的使用服务不同?
答案 0 :(得分:2)
使用此课程
txtView.setText(parseDate("06/03/2017 01:20:00","dd/MM/yyyy hh:mm:ss","dd/MM/yyyy hh:mm:ss"));
public static String parseDate(String Date, String CurrentPattern, String OutputPattern) {
SimpleDateFormat sdf = new SimpleDateFormat(CurrentPattern, Locale.getDefault());
try {
Date startDate = increaseDate(sdf.parse(Date));
sdf.applyPattern(OutputPattern);
return sdf.format(startDate);
} catch (Exception e) {
return "";
}
}
public static Date increaseDate(Date origDate) {
final Calendar calendar = Calendar.getInstance();
calendar.setTime(origDate);
calendar.add(Calendar.SECONDS, +1);
Date newDate = calendar.getTime();
return newDate;
}
并订阅
等组件中的事件import { EventEmitter, Injectable } from "@angular/core";
@Injectable()
export class SharedService {
private emitter: EventEmitter<any>;
constructor() {
this.emitter = new EventEmitter<any>();
}
getEmitter(): EventEmitter<any> {
return this.emitter;
}
emit(data: any): void {
this.emitter.emit(data);
}
}
不要忘记将其添加到NgModule并将其用作服务。