我有一个布局页面,其中包含 mat-sidenav-container 和 router-outlet。我尝试调用该函数并将数据从 router-outlet Page By EventEmitter 传递到 LayoutPage 但它不起作用。我在这里发现了与我类似的问题: @Output with EventEmitter from child toparent, view does not update in Parent
如果将 router-outlet 标签替换为组件选择器标签(app-child)就可以了,但是 router-outlet 标签对我来说是必需的。我需要通过 router-outlet 标签导航网页。那我该怎么办?
布局.组件.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>
Run Code Online (Sandbox Code Playgroud)
布局.组件.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");
}
}
Run Code Online (Sandbox Code Playgroud)
子组件.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");
}
}
Run Code Online (Sandbox Code Playgroud)
小智 6
<router-outlet (onDatePicked)='doSomething($event)' > </router-outlet>
这将不起作用,因为onDatePicked输出不存在RouterOutlet(参见Angular 文档)。
如果希望子组件与父组件之间的通信不受路由限制,则需要实现一个服务。它可以看起来像这样:
子到父.service.ts
@Injectable({
providedIn: 'root'
})
export class ChildToParentService {
onDatePicked$ = new Subject<any>();
constructor() { }
}
Run Code Online (Sandbox Code Playgroud)
布局.组件.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");
}
}
Run Code Online (Sandbox Code Playgroud)
子组件.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');
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3036 次 |
| 最近记录: |