最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

html - Import javascript file in svelte - Stack Overflow

programmeradmin1浏览0评论

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");
  }
}

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");
  }
}
Share Improve this question edited Jul 1, 2019 at 16:28 Dynamicnotion asked Jul 1, 2019 at 16:23 DynamicnotionDynamicnotion 3131 gold badge2 silver badges7 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 29

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>
发布评论

评论列表(0)

  1. 暂无评论