调用typescript组件函数到scss文件(Angular )

rt4zxlrg  于 2022-12-14  发布在  TypeScript
关注(0)|答案(1)|浏览(131)

我试图调用这个自动检测功能从我的组件.ts文件到我的.scss文件,我不知道如何或如果它甚至是可能的。
example.component.scss

.content {
  display: flex;
  align-items: center;
  text-align: center;
  height: getScreenHeight;   <--- ERROR
  width:  getScreenWidth;    <--- ERROR
}

example.component.ts

import { Component, OnInit, HostListener } from '@angular/core';

@Component({
  selector: 'app-example',
  templateUrl: './example.component.html',
  styleUrls: ['./example.component.scss']
})
export class exampleComponent implements OnInit {
  public getScreenWidth: any;
  public getScreenHeight: any;
  constructor() { }

  ngOnInit(): void {
    this.getScreenWidth = window.innerWidth;
    this.getScreenHeight = window.innerHeight;
  }

  @HostListener('window:resize', ['$event'])
  onWindowResize() {
    this.getScreenWidth = window.innerWidth;
    this.getScreenHeight = window.innerHeight;
  }
}

作为参考,我使用此功能detection
尝试在html和. scss中调用。没有任何效果,也没有发现类似的问题

8i9zcol2

8i9zcol21#

CSS不是动态的,所以从SCSS文件中处理它会给你带来问题,而不是使用内联样式来解决这些问题。

<div class="content" [ngStyle]="{ height: getScreenHeight width: getScreenWidth }">
    <!--content inside here-->
</div>

相关问题