简体   繁体   English

TypeError:无法读取 null 的属性(读取“scrollHeight”)| Angular 11

[英]TypeError: Cannot read properties of null (reading 'scrollHeight') | Angular 11

I really hope that some of you could help me to resolve this head-scratcher.我真的希望你们中的一些人能帮助我解决这个令人头疼的问题。 I need to have the scrollHeight of the page, but I get just 1/3 of that.我需要拥有页面的滚动高度,但我只得到了其中的 1/3。

setSize() {
    let DOMScrollable = this.elementRef.nativeElement.querySelector("div[data-scroll]");
    document.body.style.height = DOMScrollable.scrollHeight + "px";
}

console.log(this.dataScroll.nativeElement.scrollHeight)
// 1177

Analyzing the DOMSrollable.nativeElent , the real size of scrollHeight is 4587. So, using the function above I can't go down, because the scroll stops on 1177px.分析DOMSrollable.nativeElentscrollHeight的实际大小是 4587。因此,使用上面的 function 我不能 go 向下,因为滚动停止在 1177px。

Here's an abstraction of (my Typescript/Angular 12) what I'm dealing with.这是我正在处理的(我的 Typescript/Angular 12)的抽象。 This is the HTML (On my page I have 8 images)这是 HTML(在我的页面上我有 8 张图片)

<main main>
  <div data-scroll #dataScroll>
        <div class="page">
            <header>
                    <h1>Oceans</h1>
                    <img src="../assets/images/header.jpg" alt="">
            </header>
            <div class="grid">
                <a href="#" class="item item_v">
                    <div class="item__image">
                            <img src="../assets/images/1.jpg" alt="">
                            <div class="item__meta">December 23, 2020</div>
                    </div>

                    <h2 class="item__title">Octopus punches fish in the head (just because it can)</h2>
                    <p>Octopuses sometimes partner with fish to hunt, but the partnership comes with risks (for the fish, that is).</p>
                </a>
            </div>
         </div>
  </div>
</main>

Thanks in advance提前致谢

Try using getBoundingClientRect instead of scrollHeight .尝试使用getBoundingClientRect而不是scrollHeight eg:例如:

setSize() {
    let DOMScrollable = this.elementRef.nativeElement.querySelector("div[data-scroll]");
    const rect = DOMScrollable.getBoundingClientRect();
    document.body.style.height = rect.height + "px";
}

console.log(rect.height)
// 4587

暂无
暂无

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

相关问题 angular 错误类型错误:无法读取 null 的属性(读取“_rawValidators”) - angular ERROR TypeError: Cannot read properties of null (reading '_rawValidators') Angular 12 - 类型错误:无法读取 null 的属性(读取“writeValue”) - Angular 12 - TypeError: Cannot read properties of null (reading 'writeValue') TypeError:无法读取 null 的属性(读取“_rawValidators”) - TypeError: Cannot read properties of null (reading '_rawValidators') 类型错误:无法读取 null 的属性(读取“数量”) - TypeError: Cannot read properties of null (reading 'quantity') TypeError:无法读取 null 的属性(读取“writeValue”) - TypeError: Cannot read properties of null (reading 'writeValue') Angular:无法从库中加载组件:无法读取 null 的属性(读取“11”) - Angular : unable to load component from library : Cannot read properties of null (reading '11') 当我将 javascript 文件导入 angular 时,我发现 Uncaught TypeError: Cannot read properties of null (reading &#39;querySelector&#39;) - when i import javascript files to angular i found Uncaught TypeError: Cannot read properties of null (reading 'querySelector') 角度测试未捕获错误:未捕获(承诺):类型错误:无法读取 null 的属性(读取“参数”) - Angular Testing Uncaught Error: Uncaught (in promise): TypeError: Cannot read properties of null (reading 'params') 错误类型错误:无法读取 null 的属性(读取“toISOString”) - ERROR TypeError: Cannot read properties of null (reading 'toISOString') TypeError:无法读取 null 的属性(读取“获取”)FormGroup / FormArray - TypeError: Cannot read properties of null (reading 'get') FormGroup / FormArray
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM