【发布时间】:2021-04-30 09:56:35
【问题描述】:
在我的 Angular 通用应用程序中,我尝试在 api 调用成功后更新标题和元标记。
app.component.ts
import {Component, Inject, OnInit, PLATFORM_ID} from '@angular/core';
import {Meta, Title} from '@angular/platform-browser';
import {isPlatformBrowser} from '@angular/common';
import {TestService} from './test.service';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.scss']
})
export class AppComponent implements OnInit {
constructor(private testService: TestService,
private title: Title,
private meta: Meta,
@Inject(PLATFORM_ID) private platformId: Object) {
}
ngOnInit(): void {
if (isPlatformBrowser(this.platformId)) {
this.testService.getData().subscribe((res: any) => {
console.log(res);
this.title.setTitle(res.name);
this.meta.updateTag({property: 'og:title', content: res.name});
});
}
}
}
在服务中...
getData() {
return this.http.get('assets/data.json');
}
标题和标签在检查元素中更新,但不在查看页面源中。
欢迎提出任何建议。
【问题讨论】: