Angular-在加载路由器之前获取路由器url



我想在其他网页上将我的应用程序用作iframe。然而,因此,我需要一个"剥离"版本的应用程序,其中一些功能被禁用。

通常,我使用url/#/导航。。。对于iframe,它是url/#/iframe/。它们都导航到相同的页面,尽管后者禁用了某些功能。

我的问题是,我想在router-outlet周围放置一个div和一个类,这取决于我是否处于iframe模式。此时此刻,我只能想到检查

this.route.snapshot.url[0].path === 'iframe'

在car.component.ts文件中,将该值作为appService.iframe存储在app.service.ts中的某个位置,然后在app.component.html中使用该值。

然而,这会产生以下错误:

ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked. Previous value: 'body1'. Current value: 'body2'.

我可以在加载路由器之前获取url吗?

app.routes.ts文件:

// Route Configuration
export const routes: Routes = [
{path: '', component: CarList},
{path: 'iframe', component: CarList},
{path: 'iframe/:loket', component: Car},
{path: ':loket', component: Car}
];
// Export routes
export const routing = RouterModule.forRoot(routes);

app.component.html文件:

<div class="container">
<div class="header">
<header></header>
</div>  
<div [ngClass]="(!appService.iframe) ? 'body1': 'body2'">
<router-outlet></router-outlet>
</div>
<div class="footer">
<footer></footer>
</div>    
</div>

使用本机location.hash,您可以确定它是否包括iframe值:

location.hash.includes("iframe")

最新更新