首页 > 解决方案 > 根据当前窗口位置动态设置组件的 styleUrls

问题描述

我正在尝试styleUrls根据条件设置角度分量值window.location.href.includes('en')

home-page.component.ts

let condition = window.location.href.includes('en');

@Component({
    selector: 'app-home-page',
    templateUrl: './home-page.component.html',
    styleUrls: [condition ? './home-page.component.scss' : './home-page.component-rtl.scss'],
    encapsulation: ViewEncapsulation.None
})
export class HomePageComponent implements OnInit {
    //....
}

但我得到一个错误error NG1010: styleUrls must be an array of strings

我已经尝试将第一行更改为另一个条件,即:let condition = 1 + 1 == 2;,但没有显示错误。它仅在我window.SOMETHING在条件内使用时显示。

随着我进行更多调查,出现错误window是因为在组件加载之前未定义。

如何styleUrls根据取决于值的条件设置window.location.href值?在加载组件之前
如何访问?window

标签: angulartypescriptangular8

解决方案


推荐阅读