Angular 10,带有EventEmitter的路由器出口输出到SideNav内容中的父级



我有一个Layout Page,其中包含sidenav容器和路由器出口。我试图调用该函数并将数据从路由器出口Page By EventEmitter传递到LayoutPage,但它不起作用。我在这里发现了类似的问题:@使用EventEmitter从子对象输出到父对象,视图没有';t在父中更新

如果将路由器出口标签替换为组件选择器标签(应用程序子标签(,这是可行的,但路由器出口标签对我来说是必要的。我需要通过路由器出口标签导航网页。那我该怎么办?

layout.component.html

<mat-sidenav-container>
<mat-sidenav #sideNav opened="true" mode="side" class="sidebar" style="background-image: url('assets/images/test.png');">
<ul class="nav">
<li routerLinkActive="active" *ngFor="let menuItem of menuItems" class="test nav-item">
<a class="nav-link" [routerLink]="[menuItem.path]" [queryParams]="filter">
<i class="material-icons">{{menuItem.icon}}</i>
<p>{{menuItem.title}}</p>
</a>
</li>
</ul>
</mat-sidenav>

<mat-sidenav-content class="content">
<app-header></app-header>
<router-outlet (onDatePicked)='doSomething($event)' > </router-outlet>

</mat-sidenav-content>
</mat-sidenav-container>

布局.组件.ts

@Component({
selector: 'app-layout',
templateUrl: './layout.component.html',
styleUrls: ['./layout.component.css']
})
export class ChemoLayoutComponent implements OnInit {
filter:Filter
menuItems: any[];
constructor(private route: ActivatedRoute,private app: ApplicationRef) {
this.menuItems = [
{ path: '/chemo/child', title: 'aaa',  icon: 'library_books' },
{ path: '/chemo/child1', title: 'bbb',  icon:'build'},
{ path: '/chemo/child2', title: 'ccc',  icon:'search' },
];
this.filter = new Filter();
}
ngOnInit(): void {
this.route.queryParams.subscribe((params) => {
this.filter.CHARTNO = params['CHARTNO'];
});
}
doSomething($event){  //<== It's not working
this.app.tick();
console.log("Test");
}
}

子组件.ts

@Component({
selector: 'app-child',
templateUrl: './child.component.html',
styleUrls: ['./child.component.css']
})
export class ChildComponent implements OnInit {
@Output() onDatePicked: EventEmitter<any> = new EventEmitter<any>();
constructor() {
}
ngOnInit(): void {
this.onDatePicked.emit("hello");
}
}

<router-outlet (onDatePicked)='doSomething($event)' > </router-outlet>

这将不起作用,因为RouterOutlet上不存在onDatePicked输出(参见Angular doc(。

如果您希望在不受路由限制的情况下从子组件到父组件进行通信,则需要实现服务。它可以看起来像这样:

子级到父级.service.ts

@Injectable({
providedIn: 'root'
})
export class ChildToParentService {
onDatePicked$ = new Subject<any>();
constructor() { }
}

布局.组件.ts

@Component({
selector: 'app-layout',
templateUrl: './layout.component.html',
styleUrls: ['./layout.component.css']
})
export class ChemoLayoutComponent implements OnDestroy {
onDatePickedSub: Subscription;
constructor(
private childToParentService: ChildToParentService
) {
this.onDatePickedSub = this.childToParentService.onDatePicked$.subscribe($event => {
this.doSomething($event);
});
}
ngOnDestroy(): void {
if (this.onDatePickedSub) {
this.onDatePickedSub.unsubscribe();
}
}
doSomething($event): void {
this.app.tick();
console.log("Test");
}
}

子组件.ts

@Component({
selector: 'app-child',
templateUrl: './child.component.html',
styleUrls: ['./child.component.css']
})
export class ChildComponent implements OnInit {
constructor(
private childToParentService: ChildToParentService
) { }
ngOnInit(): void {
this.childToParentService.onDatePicked$.next('hello');
}
}

最新更新