【发布时间】:2018-10-29 15:32:52
【问题描述】:
我的页脚在内容加载之前被加载。我的导航栏中有多个按钮,单击时会打开一个新组件。当用户点击事件时,它会在从 api 加载事件后发出。此时页脚加载正常。
但是在那之后我去另一个链接让我们说特殊然后页脚在事件之前加载。
我尝试如下:
events.component.ts
export class EventsComponent implements OnInit {
events = [];
constructor(private _eventService: EventService, private service: SharedService) { }
ngOnInit() {
this._eventService.getEvents()
.subscribe(
res => {
this.events = res,
this.service.footer.emit(),
},
err => console.log(err)
);
}
}
shared.service
import { Injectable, EventEmitter, Output } from '@angular/core';
@Injectable({
providedIn: 'root'
})
export class SharedService {
@Output() footer = new EventEmitter<any>();
constructor() {}
}
app.component.ts
export class AppComponent {
flag: boolean;
constructor(private service: SharedService) {
this.service.footer.subscribe(() => {
this.flag = true ;
console.log("Flag is ::: ",this.flag);
})
}
}
app.component.html
<app-navbar></app-navbar>
<router-outlet></router-outlet>
<div class="footer" *ngIf="flag">
<app-footer></app-footer>
</div>
这是一个 Stackblitz: https://stackblitz.com/edit/angular-662ndm
【问题讨论】:
-
我不完全确定您要完成什么,但看起来您正在尝试使用标志变量来控制页脚的显示?每次有人导航时都需要将其重置为 false 吗?
-
不明白你的问题,预期的行为是什么?
-
@lupus137 是的,该标志每次都设置为 true,因此当另一个页面在内容之前导航页脚时显示。
-
所以您只需要先重置该字段。我会推荐一种方法,但我目前不太了解设置。
-
@SS 我认为您的 ngOnInit 没有在路由更改时触发。请检查我的答案。