본문 바로가기

개발하자

노드 모듈을 svelte 구성 요소로 가져오려면 어떻게 해야 합니까

반응형

노드 모듈을 svelte 구성 요소로 가져오려면 어떻게 해야 합니까

나는 처음이고 나의 의존성에 모멘텀슬라이더라고 불리는 설치된 노드 모듈을 사용하려고 한다. 내 svelt 구성 요소의 스크립트 태그에는 다음이 있습니다:

import MomentumSlider from "../../node_modules/momentum-slider";
let slider = new MomentumSlider({
   el: ".ms-container",
});

내 컴포넌트의 html 마크업에서 나는 다음의 튜토리얼에 나와 있는 것처럼 마크업을 제안한다

However, I am getting a typeError in the browser console:

enter image description here

I am new to development in general and I am not sure if this is a problem with momentum-slider or an error on my part. Any insights would be much appreciated.




If you have installed the package properly: npm install momentum-slider the package is listed in your package.json.

When this fits, you just have to import: import MomentumSlider from "momentum-slider";




Not sure how to use this library but you should take care of 2 things. First import your package like the following:

import MomentumSlider from "momentum-slider";

Second you need to initialise the MomentumSlider class when the component is mounted using onMount:

import { onMount } from "svelte";
import MomentumSlider from "momentum-slider";

let slider;

onMount(() => {
  slider = new MomentumSlider({ 
    el: ".ms-container"
  });
});

Edit exciting-bouman-dyywc




For future reference, some packages use the require method in their documentation for usage in your work. Svelte doesn't really like require so a good alternative is

Instead of: var Validator = require('jsonschema').Validator;

Do this: import { Validator } from "jsonschema";


반응형