在刷新页面以保存已动态添加的标记时,是否存在这种可能性?
现在,当我刷新页面时,在加载时,标题标记会更改为原始标记,这是我在index.html中设置的。加载页面后,标题标签将返回到动态添加的正确标记。但是,当页面刷新时,我希望标题标签保持不变。
这是我的app.component.ts:
this.router.events.pipe(
filter((event) => event instanceof NavigationEnd),
map(() => this.activatedRoute),
map((route) => {
while (route.firstChild) route = route.firstChild;
return route;
}),
filter((route) => route.outlet === 'primary'),
mergeMap((route) => route.data)
)
.subscribe((event) => {
console.log(event)
this.translateService.get(event['title']).subscribe(name => {
this._seoService.updateTitle(name);
});
this._seoService.updateDescription(event['description'])
});发布于 2021-12-13 13:06:08
一种方法是使用局部存储将动态标题存储在其中。下面是一个简单的示例,其中我将标题存储在本地存储中并刷新页面,并将标题保留回原处。Angular提供了一个名为标题的服务,它允许我们随时动态更新标题。
<button (click)="setItem()">Click to set a title</button>
<p *ngIf="showInfo" >Refresh the page now :)</p>export class AppComponent implements OnInit {
showInfo = false;
constructor(private titleService: Title) {}
ngOnInit() {
this.getItem();
}
setItem() {
localStorage.setItem('title', 'Hey World!');
this.showInfo = true;
this.getItem();
}
getItem() {
if (localStorage.getItem('title'))
this.titleService.setTitle(localStorage.getItem('title'));
else this.titleService.setTitle('No title');
}
}这是一个现场应用。
代码- 斯塔克布利茨
https://stackoverflow.com/questions/70334660
复制相似问题