简体   繁体   English

如何获得角度为 8 的 ion-col 的大小

[英]how to get the size of an ion-col in angular 8

I'm trying to get the ion-col size in angular 8我正在尝试获得角度为 8 的 ion-col 大小

My code works very well if I want the size of a <div> but doesn't work if I want from <ion-col> .如果我想要<div>的大小,我的代码工作得很好,但如果我想要<ion-col>则不起作用。

I get this error message:我收到此错误消息:

ERROR TypeError: Cannot read property 'offsetWidth' of undefined错误类型错误:无法读取未定义的属性“offsetWidth”

Someone have an idea if it's possible to do it and how to do ?有人知道是否可以这样做以及如何做?

Thanks you very much for help非常感谢您的帮助

CSS Code: CSS 代码:

div {
    margin: auto;
    height: 50px;
    border: 1px solid black;
}

TS Code:代码:

  @ViewChild('container', { static: true }) container: ElementRef;

  getInfo() {
    const width = this.container.nativeElement.offsetWidth;
    const height = this.container.nativeElement.offsetHeight;
    console.log('width : ', width, ' + ', height);
  }

HTML Code: HTML代码:

<ion-content>

  <ion-grid>
    <ion-row>

      <ion-col size="6" (click)="getInfo()" #container>
        <div></div>
      </ion-col>

    </ion-row>
  </ion-grid>

</ion-content>

That's because <ion-col> is not a ElementRef那是因为<ion-col>不是ElementRef

You should have something like :你应该有类似的东西:

@ViewChild('container', {read: ElementRef}) container: ElementRef;

And then :接着 :

this.container.nativeElement.offsetHeight

this work for me in ionic 5这在离子 5 中对我有用

TS CODE代码

@ViewChild('container', {read: ElementRef}) container: ElementRef;



  getInfo() {
    const width = this.container.el.offsetWidth; // instead of this.container.nativeElement.offsetWidth;
    const height = this.container.el.offsetHeight;
    console.log('width : ', width, ' + ', height);
  }

HTML CODE代码


    <ion-row>
      <ion-col size="12" #container>
        .....
      </ion-col>
    </ion-row>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM