Import javascript file in svelte

情到浓时终转凉″ 提交于 2019-12-10 15:46:49

问题


So today I discovered Svelte and I absolutley love the concept. I only got one problem I wrote a small helper.js file and can't seem to import it. Every time I try to reference the class I get

ReferenceError: Helper is not defined


main.js file:

import App from './App.svelte';
import './helper.js';

var app = new App({
    target: document.body
});
export default app;


App.svelte file:

<script>
    let helper = new Helper();
</script>

<h1>Hello</h1>


helper.js file:

class Helper {
  constructor() {
    console.log("working");
  }
}

回答1:


You need to import it into the file that uses it:

<script>
  import Helper from './helper.js';
  let helper = new Helper();
</script>

<h1>Hello</h1>


来源:https://stackoverflow.com/questions/56839098/import-javascript-file-in-svelte

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!