我想知道如何在像Polymer 3 / LitElement这样的Web组件中实现noUiSlider,我尝试了下面的代码,它得到了一个错误,因为nouislider.create is not function
和服务器以non-JavaScript MIME type of "text/css"
的响应结束。
import { LitElement, html } from 'https://unpkg.com/@polymer/lit-element/lit-element.js?module';
import * as noUiSlider from 'https://unpkg.com/nouislider/distribute/nouislider.min.js'
export class Filters extends LitElement {
constructor() {
super();
this.slider();
}
slider(){
var range = document.getElementById('range');
noUiSlider.create(range, {
start: [ 20, 80 ], // Handle start position
step: 10, // Slider moves in increments of '10'
margin: 20, // Handles must be more than '20' apart
connect: true, // Display a colored bar between the handles
direction: 'rtl', // Put '0' at the bottom of the slider
orientation: 'vertical', // Orient the slider vertically
behaviour: 'tap-drag', // Move handle on tap, bar is draggable
range: { // Slider can select '0' to '100'
'min': 0,
'max': 100
},
pips: { // Show a scale with the slider
mode: 'steps',
density: 2
}
});
var valueInput = document.getElementById('value-input'),
valueSpan = document.getElementById('value-span');
// When the slider value changes, update the input and span
range.noUiSlider.on('update', function( values, handle ) {
if ( handle ) {
valueInput.value = values[handle];
} else {
valueSpan.innerHTML = values[handle];
}
});
// When the input changes, set the slider value
valueInput.addEventListener('change', function(){
range.noUiSlider.set([null, this.value]);
});
}
render() {
return html`
<div id="range"></div>
`;
由于HTML的渲染时间,抛出错误。所以你可以使用如下; *另外检查如何引用阴影dom this.shadowRoot.getElementById('range')
中的元素而不是document.getElementById
import { LitElement, html } from '@polymer/lit-element';
import {afterNextRender } from '@polymer/polymer/lib/utils/render-status.js';
import * as noUiSlider from 'https://unpkg.com/nouislider/distribute/nouislider.min.js'
class MyFilters extends LitElement {
constructor() {
super();
afterNextRender(this, ()=>{
this.slider();
})
}
slider(){
var range = this.shadowRoot.getElementById('range');
noUiSlider.create(range, {
start: [ 20, 80 ], // Handle start position
step: 10, // Slider moves in increments of '10'
margin: 20, // Handles must be more than '20' apart
connect: true, // Display a colored bar between the handles
direction: 'rtl', // Put '0' at the bottom of the slider
orientation: 'vertical', // Orient the slider vertically
behaviour: 'tap-drag', // Move handle on tap, bar is draggable
range: { // Slider can select '0' to '100'
'min': 0,
'max': 100
},
pips: { // Show a scale with the slider
mode: 'steps',
density: 2
}
});
var valueInput = this.shadowRoot.getElementById('value-input'),
valueSpan = this.shadowRoot.getElementById('value-span');
// When the slider value changes, update the input and span
range.noUiSlider.on('update', function( values, handle ) {
if ( handle ) {
valueInput.value = values[handle];
} else {
valueSpan.innerHTML = values[handle];
}
});
// When the input changes, set the slider value
valueInput.addEventListener('change', function(){
range.noUiSlider.set([null, this.value]);
});
}
render() {
return html`
<div id="range">
<span id="value-span"></span>
<input id="value-input"/>
</div>
`;
}
}
customElements.define('my-filters', MyFilters);