angular框架 前端获取电脑网页屏幕尺寸信息
在Angular框架中,你可以使用window对象的innerWidth和innerHeight属性来获取电脑网页屏幕的尺寸信息。下面是一个示例代码:
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'app-example',
template: `
<p>屏幕宽度: {{ screenWidth }}px</p>
<p>屏幕高度: {{ screenHeight }}px</p>
`,
styles: []
})
export class ExampleComponent implements OnInit {
screenWidth: number;
screenHeight: number;
constructor() { }
ngOnInit() {
this.screenWidth = window.innerWidth;
this.screenHeight = window.innerHeight;
}
}
在上面的代码中,我们在组件的ngOnInit生命周期钩子中获取了屏幕的宽度和高度,并将其赋值给组件的属性screenWidth和screenHeight。然后,我们可以在模板中使用这些属性来显示屏幕尺寸信息。
请注意,由于ngOnInit生命周期钩子在组件初始化完成后才会被调用,因此你可能需要在组件的构造函数中初始化screenWidth和screenHeight,以避免在模板中引用未定义的属性。
原文地址: https://www.cveoy.top/t/topic/jc02 著作权归作者所有。请勿转载和采集!