angular - 根据当前窗口位置动态设置组件的 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
解决方案
推荐阅读
- mongodb - 如何将此 MYSQL 查询写入 Mongodb?
- javascript - 从 javascript 对象中获取编码内容并将其显示在 pug 上,如何?
- rss - 如何通过插件处理 Thunderbird 中的提要文件夹?
- python - 禁止 argaparse 生成的“帮助”文本中的重复条目
- git - 如何挑选从 repo1 到 repo2 的提交
- javascript - 字符串太长时如何使字体变小?
- c# - 在 C# 中从控制台应用程序播放声音
- javascript - Angular 如何在不更改 URL 的情况下使用参数进行路由
- javascript - 在 React Native App 中收到警告“列表中的每个孩子都应该有一个唯一的 key prop”
- cmake - cmake ADD_EXECUTABLE 无法设置“../bin/xxx”