在渲染Meta标签时是否会触发事件?
在我的angular 4应用程序中,我有一些动态路由,比如:
const routes: Routes = [
{ path: 'products/:slug', component: ProductComponent }
];在ProductComponent (product.component.ts)中,我获得的元数据如下:
ngOnInit() {
let currentSlug = this.route.snapshot.paramMap.get('slug');
//Makes ajax call to server to get meta data
this.MyMetaService.getMeta(currentSlug)
.subscribe((metadata) = > {
//Set the page title
this.title.setTitle(metadata.title);
this.meta.addTags([
{name: 'keywords', content: metadata.keywords},
{name: 'description', content: metadata.description}
])
});
}它工作得很好!现在,我想知道什么时候meta标签完全呈现在页面上,但是到目前为止还不知道是怎么做到的!
我尝试过以下几种方法,但都失败了:
ngAfterViewInit() {
const title = $(document).find("title").text();
console.log('page title is: ', title); //title is still not updated!
}当然,setTimeout可以工作,但我不确定它是否可靠:
setTimeout(() => {
const title = $(document).find("title").text();
console.log('page title is: ', title); //Got correct title here!
}, 200);发布于 2017-07-30 16:59:04
这背后的原因是,您正在进行一个ajax调用来检索元标签,这需要从服务器往返的时间(可能需要几毫秒)。一旦内部组件树被呈现,ngAfterViewInit组件钩子就会被调用。
我不这么认为,setTimeout让它单独工作,它工作是因为你提到了200ms时间。
https://stackoverflow.com/questions/45397935
复制相似问题