【问题标题】:Cannot read property `contentHeight` of Undefined in Ionic 2无法读取 Ionic 2 中未定义的属性“contentHeight”
【发布时间】:2018-12-16 18:19:39
【问题描述】:

我需要在我的 Ionic 2 页面中获取 scrollTop 位置,但它无法使用简单的 jQuery。

在下面查看我的工作示例,滚动页面并单击它,它将为您提供y 位置编号。

$("body").click(function(){
    var scrollPost = $(document).scrollTop();
    alert(scrollPost);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
test<br/><br/><br/><br/>kjh<br/>kjgh<br/>uitz<br/><br/><br/><br/>uztjhg<br/>kjh<br/>
test<br/><br/><br/><br/>kjh<br/>kjgh<br/>uitz<br/><br/><br/><br/>uztjhg<br/>kjh<br/>test<br/><br/><br/><br/>kjh<br/>kjgh<br/>uitz<br/><br/><br/><br/>uztjhg<br/>kjh<br/>test<br/><br/><br/><br/>kjh<br/>kjgh<br/>uitz<br/><br/><br/><br/>uztjhg<br/>kjh<br/>test<br/><br/><br/><br/>kjh<br/>kjgh<br/>uitz<br/><br/><br/><br/>uztjhg<br/>kjh<br/>test<br/><br/><br/><br/>kjh<br/>kjgh<br/>uitz<br/><br/><br/><br/>uztjhg<br/>kjh<br/>test<br/><br/><br/><br/>kjh<br/>kjgh<br/>uitz<br/><br/><br/><br/>uztjhg<br/>kjh<br/>test<br/><br/><br/><br/>kjh<br/>kjgh<br/>uitz<br/><br/><br/><br/>uztjhg<br/>kjh<br/>
<div class="test">Thats a test</div>

但是jQuery获取scrollTop的方法不起作用,它总是返回0。

所以我打算用离子方式得到它,documentation here

我正在尝试使用 dimensions.contentHeightdimensions.contentTop,但出现错误。下面是我的代码。

home.html 代码:

<ion-content content>
    <div #topScroll>
  The world is your oyster.
  test<br/><br/><br/><br/>kjh<br/>kjgh<br/>uitz<br/><br/><br/><br/>uztjhg<br/>kjh<br/>
  test<br/><br/><br/><br/>kjh<br/>kjgh<br/>uitz<br/><br/><br/><br/>uztjhg<br/>kjh<br/>test<br/><br/><br/><br/>kjh<br/>kjgh<br/>uitz<br/><br/><br/><br/>uztjhg<br/>kjh<br/>test<br/><br/><br/><br/>kjh<br/>kjgh<br/>uitz<br/><br/><br/><br/>uztjhg<br/>kjh<br/>test<br/><br/><br/><br/>kjh<br/>kjgh<br/>uitz<br/><br/><br/><br/>uztjhg<br/>kjh<br/>test<br/><br/><br/><br/>kjh<br/>kjgh<br/>uitz<br/><br/><br/><br/>uztjhg<br/>kjh<br/>test<br/><br/><br/><br/>kjh<br/>kjgh<br/>uitz<br/><br/><br/><br/>uztjhg<br/>kjh<br/>test<br/><br/><br/><br/>kjh<br/>kjgh<br/>uitz<br/><br/><br/><br/>uztjhg<br/>kjh<br/>
  <div class="test">Thats a test</div>
</div>
</ion-content>

编辑 1

home.ts 代码更新

home.ts 代码:

    import { Content } from 'ionic-angular';
    import { Component, ViewChild } from '@angular/core';
    import { NavController } from 'ionic-angular';
    import { AfterViewInit,Renderer2, ElementRef } from '@angular/core';

    @Component({
      selector: 'page-home',
      templateUrl: 'home.html'
    })

    export class HomePage implements AfterViewInit  {

      @ViewChild(Content) content: Content;
    constructor(private renderer: Renderer2, public navCtrl: NavController) {       
    }

ngAfterViewInit() {

    // Renderer2 used not Renderer, inject it before using
    this.renderer.listen('document', 'click', (e) => {
      // Get contentHeight here
      var iscroll =  this.content.contentHeight;
// error - Property 'dimensions' does not exist on type 'Content'
      var iscroll2 = this. content.dimensions.contentHeight;
      alert(iscroll);
      alert(iscroll2);
    });
    }

出现错误 - 无法读取未定义的属性 contentHeight

我做错了什么?

【问题讨论】:

  • 哪一行? iscrolliscroll2?
  • 您正在尝试获取未定义元素的高度,这意味着该元素不存在。试试$(document).ready(function() { //GET HEIGHT HARE } ); 顺便说一下&lt;div #topScroll&gt; 不应该是&lt;div id="#topScroll"&gt;

标签: jquery angular ionic-framework ionic2 ionic3


【解决方案1】:

这里有多个问题:

  • 您正在使用 jQuery 点击处理程序
  • 您在模板中使用的是content 而不是#content

我愿意:

<ion-content #content><!-- omitted content --></ion-content>

// Make sure to import AfterViewInit and implement it
ngAfterViewInit() {

  // Renderer2 used not Renderer, inject it before using
  this.renderer.listen('document', 'click', (e) => {
    // Get contentHeight here
  });
}

【讨论】:

  • 按照您的指导实施,错误来了 - Property 'dimensions' does not exist on type 'Content',更新我的代码
  • 更新了我的home.ts 代码,请进一步查看和指导
  • 现在到底发生了什么 - 在离子服务上,alert 给出一个值然后错误 - Cannot read property 'contentHeight' of undefined,如果我滚动页面它说警报说[object object]
  • @user2828442 您是否检查过this.contentcontentHeight 属性的实际位置?因为你现在用的是两种方式,所以有点奇怪。
  • 再次做 ionic serve 我的缓存问题,一分钟告诉你
猜你喜欢
  • 2017-11-06
  • 1970-01-01
  • 1970-01-01
  • 2017-10-07
  • 2015-04-25
  • 1970-01-01
  • 2019-07-31
  • 2019-11-11
  • 2023-03-24
相关资源
最近更新 更多