0

我想将我的应用程序用作其他网页上的 iframe。但是,因此我需要我的应用程序的“剥离”版本,其中某些功能被禁用。

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

我的问题是我想根据我是否处于 iframe 模式来放置一个类divrouter-outlet这一刻,我只能想到检查

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

在 car.component.ts 文件中,将此值存储在 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>
4

1 回答 1

1

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

location.hash.includes("iframe")
于 2018-12-13T20:34:33.553 回答