简体   繁体   English

mat-slider - Angular 材料实验 - _getHostElement 错误。 如何解决?

[英]mat-slider - Angular Material experimental - _getHostElement error. How to resolve?

I have recently upgraded to Angular 12 and installed Angular Material Experimental in order to try out the new mat-slider.我最近升级到 Angular 12 并安装了 Angular Material Experimental 以试用新的 mat-slider。 I effectively want a range slider, which is not available in the current angular material package, and I'd prefer to stay within the Angular Material ecosystem.我实际上想要一个范围 slider,它在当前 angular 材料 package 中不可用,我更愿意留在 Angular 材料生态系统中。

From the following discussion is why I decided to try the new mat-slider in material experimental https://github.com/angular/components/issues/1331从以下讨论中可以看出为什么我决定在材料实验https://github.com/angular/components/issues/1331中尝试新的 mat-slider

Code:代码:

Module:模块:

import { MatSliderModule } from '@angular/material-experimental/mdc-slider'; with the appropriate imports etc与适当的进口等

HTML: <mat-slider discrete markers thumbLabel [min]="0" [max]="100" [step]="5" values="[10,20]"></mat-slider> HTML: <mat-slider discrete markers thumbLabel [min]="0" [max]="100" [step]="5" values="[10,20]"></mat-slider>

GUI: I get the following visual - just a back bar for the slider img GUI:我得到以下视觉效果 - 只是 slider img的后栏

Error: And the console error:错误: 控制台错误:

main.js:1 ERROR TypeError: Cannot read property '_getHostElement' of undefined
    at me.value (main.js:1)
    at Object.Lt.setThumbStyleProperty (main.js:1)
    at main.js:1
    at main.js:1
    at ae.<computed> (polyfills.js:1)
    at X.invokeTask (polyfills.js:1)
    at Object.onInvokeTask (main.js:1)
    at X.invokeTask (polyfills.js:1)
    at X.runTask (polyfills.js:1)
    at X.invokeTask (polyfills.js:1)

Normal Angular Material mat-sliders are not an issue to get working.正常 Angular 材料垫滑块不是开始工作的问题。

Is there something obvious that I am missing to get the _getHostElement error?有什么明显的东西让我缺少_getHostElement错误吗? Maybe another module to import?也许要导入另一个模块? I can't seem to find anything related to this error for Mat-Slider in experimental.我似乎无法在实验中找到与 Mat-Slider 的此错误相关的任何内容。

I can confirm I also see this same error using the most basic experimental mat-slider.我可以确认使用最基本的实验垫滑块我也看到了同样的错误。 I am successfully using other experimental controls, it is just the slider that refuses to render correctly.我成功地使用了其他实验控件,只是 slider 拒绝正确渲染。 This has been the case for quite a while and isn't a new issue.这种情况已经有一段时间了,并不是一个新问题。

I ran into the same problem.我遇到了同样的问题。 Apparently they have updated the code since you asked this question, because they now give you a hint in the console:显然,自从您提出这个问题以来,他们已经更新了代码,因为他们现在在控制台中给您一个提示:

   Valid configurations are as follows:
    <mat-slider>
      <input matSliderStartThumb>
      <input matSliderEndThumb>
    </mat-slider>

You should probably use a dual range slider CSS customizatión since Angular material presents sometimes problems, try this:您可能应该使用双范围 slider CSS 定制,因为 Angular 材料有时会出现问题,试试这个:

HTML HTML

<div class="range_container">
    <div class="sliders_control">
        <input id="fromSlider" type="range" value="10" min="0" max="100"/>
        <input id="toSlider" type="range" value="40" min="0" max="100"/>
    </div>
    <div class="form_control">
        <div class="form_control_container">
            <div class="form_control_container__time">Min</div>
            <input class="form_control_container__time__input" type="number" id="fromInput" value="10" min="0" max="100"/>
        </div>
        <div class="form_control_container">
            <div class="form_control_container__time">Max</div>
            <input class="form_control_container__time__input" type="number" id="toInput" value="40" min="0" max="100"/>
        </div>
    </div>
</div>

CSS: CSS:

    .range_container {
  display: flex;
  flex-direction: column;
  width: 80%;
  margin: 35% auto;
}

.sliders_control {
  position: relative;
  min-height: 50px;
}

.form_control {
  position: relative;
  display: flex;
  justify-content: space-between;
  font-size: 24px;
  color: #635a5a;
}

input[type=range]::-webkit-slider-thumb {
  -webkit-appearance: none;
  pointer-events: all;
  width: 24px;
  height: 24px;
  background-color: #fff;
  border-radius: 50%;
  box-shadow: 0 0 0 1px #C6C6C6;
  cursor: pointer;
}

input[type=range]::-moz-range-thumb {
  -webkit-appearance: none;
  pointer-events: all;
  width: 24px;
  height: 24px;
  background-color: #fff;
  border-radius: 50%;
  box-shadow: 0 0 0 1px #C6C6C6;
  cursor: pointer;  
}

input[type=range]::-webkit-slider-thumb:hover {
  background: #f7f7f7;
}

input[type=range]::-webkit-slider-thumb:active {
  box-shadow: inset 0 0 3px #387bbe, 0 0 9px #387bbe;
  -webkit-box-shadow: inset 0 0 3px #387bbe, 0 0 9px #387bbe;
}

input[type="number"] {
  color: #8a8383;
  width: 50px;
  height: 30px;
  font-size: 20px;
  border: none;
}


input[type=number]::-webkit-inner-spin-button, 
input[type=number]::-webkit-outer-spin-button {  
   opacity: 1;
}

input[type="range"] {
  -webkit-appearance: none; 
  appearance: none;
  height: 2px;
  width: 100%;
  position: absolute;
  background-color: #C6C6C6;
  pointer-events: none;
}

#fromSlider {
  height: 0;
  z-index: 1;
}

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

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