[英]How to import js file into svelte
I am building an app in svelte and I have a helper.js file inside /src folder where I am going to put helper functions.我正在用 svelte 构建一个应用程序,我在 /src 文件夹中有一个 helper.js 文件,我将在其中放置辅助函数。
I am trying to import the helper.js file into the GetData.svelte file:我正在尝试将 helper.js 文件导入 GetData.svelte 文件:
<script>
import Helper from "./helper.js";
let helper = new Helper();
</script>
class Helper {
constructor() {
console.log("hello world");
}
}
import App from './App.svelte';
const app = new App({
target: document.body,
});
export default app;
However, the error message I get is:但是,我收到的错误消息是:
==>> Uncaught ReferenceError: Helper is not defined (main.js 6) ==>> 未捕获的 ReferenceError:Helper 未定义(main.js 6)
Do I have to import the helper.js file in the main.js as well?我是否也必须在 main.js 中导入 helper.js 文件?
Thank you for any help.感谢您的任何帮助。
You need to export your class in helper.js :你需要在helper.js 中导出你的类:
export class Helper {
// ...
}
and then import it in main.js as然后将其导入main.js作为
import { Helper } from "./helper.js";
Or, if you only want to export this one class from the helper file, you can use或者,如果您只想从帮助文件中导出这一类,您可以使用
export default class Helper {
// ...
}
and import as you currently do:并像您目前所做的那样导入:
import Helper from "./helper.js";
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.