[英]Angular - Get router url before loading router
我想将我的应用程序用作其他网页上的iframe。 但是,因此我需要我的应用程序的“ stripped”版本,其中某些功能被禁用。
通常,我使用url /#/ ...导航。对于iframe,它是url /#/ iframe / ...。 它们都导航到相同的页面,尽管在后者中某些功能被禁用。
我的问题是,我想根据是否处于iframe模式将div
放在router-outlet
周围,并放置一个类。 目前,我只能想到检查
this.route.snapshot.url[0].path === 'iframe'
在car.component.ts文件中,将该值作为appService.iframe
存储在app.service.ts中的某个appService.iframe
,然后在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>
使用native location.hash
可以确定它是否包含iframe值:
location.hash.includes("iframe")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.