【发布时间】:2018-09-13 17:06:10
【问题描述】:
我正在构建将与外部 CMS 集成的 Angular 5 应用程序。该 CMS 具有被重写为我的 Angular 应用程序的页面模板。它的工作方式如下面的屏幕所示:
几乎一切都很好,只有我有闪烁的问题。当我第一次进入页面时,我可以看到容器和布局加载之间闪烁。请参阅下面的屏幕:
这是我的代码:
app.module.ts(路由配置)
const routes: Routes = [
{
path: '',
pathMatch: 'full',
redirectTo: '/pl'
},
{
path: ':lang',
component: ContainerComponent
},
{
path: ':lang/:index',
component: ContainerComponent,
},
{
path: '**',
component: NotfoundComponent
}
];
container.component.ts
@Component({
selector: 'app-container',
templateUrl: './container.component.html',
providers: [DownloadService, ServerService, Config, SeoService, LinkService],
})
export class ContainerComponent implements OnDestroy, AfterContentInit {
subscription: ISubscription;
lang: string;
@ViewChild('container', { read: ViewContainerRef }) _vcr;
constructor(@Inject(PLATFORM_ID) private platformId: Object, private link: LinkService, private componentFactoryResolver: ComponentFactoryResolver, private route: ActivatedRoute, private dl: DownloadService, private service: ServerService, private config: Config, private seo: SeoService) {
if (isPlatformBrowser(this.platformId))
this.getData();
}
ngOnInit() {
}
ngAfterContentInit() {
this.subscription = this.route.params.subscribe((params) => {
this.getLayoutData(params);
})
}
ngOnDestroy() {
if (this.subscription) this.subscription.unsubscribe();
}
generateSeo(seoData: Seo, langURL, index) {
let title = '';
if (seoData.metaTitle == '')
title = seoData.title;
else
title = seoData.metaTitle;
this.link.addTag({ rel: 'canonical', href: `${this.config.URL}/${langURL}/${index}` });
this.seo.generateTags({ lang: langURL, title: title, description: seoData.description, keywords: seoData.keywords, image: seoData.banner, slug: index })
}
getLayout(index): Type<any> {
switch (index) {
case 'HomeComponent': return HomeComponent
case 'MainComponent': return MainComponent
case 'NewsComponent': return NewsComponent
default: return MainComponent
}
}
getLayoutData(params) {
let index = '';
if (typeof params.index === 'undefined') index = 'home';
else index = params.index;
if (typeof params.lang === 'undefined') this.lang = this.config.getLanguage();
else this.lang = params.lang;
this.subscription = this.service.getResponse(`${this.config.impressURL}/api/seo/${index}/${this.lang}`).subscribe(response => {
this.generateSeo(response, this.lang, index);
}, (error) => {
console.log(error);
});
if (isPlatformBrowser(this.platformId))
this.subscription = this.service.getResponse(`${this.config.impressURL}/api/layout/${index}/${this.lang}`).subscribe(res => {
this.getComponent(res);
}, (error) => {
this.dl.getLayout(URL, index, params.lang).then((res: any) => {
this.getComponent(res);
});
});
if (isPlatformServer(this.platformId))
this.subscription = this.service.getResponse(`${this.config.impressURL}/api/layout/${index}/${this.lang}`).subscribe(res => {
this.getComponent(res);
});
}
getComponent(layout) {
let component = this.getLayout(layout);
let componentFactory = this.componentFactoryResolver.resolveComponentFactory(component);
let viewContainerRef = this._vcr;
viewContainerRef.clear();
let componentRef = viewContainerRef.createComponent(componentFactory);
}
async getData() {
await this.dl.downloadDataInBackground(this.config.impressURL);
}
}
container.component.html
<div class="mainContainer">
<div #container></div>
</div>
app.component.html
<app-menu></app-menu>
<main class="main-content">
<router-outlet></router-outlet>
</main>
<app-footer></app-footer>
编辑 11:04 04.04.2018 - container.component.html
现在 container.component.html 看起来像
<div class="mainContainer">
<ng-template #container [ngIf]="layout"></ng-template>
</div>
你知道怎么解决吗?
【问题讨论】:
-
发布您的 html。考虑使用 *ngIf 或微调器。如果您的数据尚未加载,ngIf 将阻止页面呈现。
-
好吧,我在主帖中添加了 html。嗯..我没有那样想。也许您的简单解决方案可以解决问题。我去看看。
-
我不能在 ViewContainerRef 甚至在同一个标签中使用 ngIf。
-
在它周围再放一个 div 吗?
-
我不能。会出现
core.js:1448 ERROR TypeError: Cannot read property 'clear' of undefined之类的错误。clear()是来自ViewContainerRef的方法
标签: javascript angular animation components pageload