将JS类导入苗条组件的正确方法是什么?
我有一个someclass.ts
喜欢:
class SomeClass {
constructor () {
console.log('created someclass')
}
}
export default SomeClass
哪个将someclass.js
汇编为
'use strict'
exports.__esModule = true
const SomeClass = /** @class */ (function () {
function SomeClass () {
console.log('created someclass')
}
return SomeClass
}())
exports.default = SomeClass
app.svelte
喜欢
<script>
import SomeClass from "./SomeClass";
const instance = new SomeClass();
</script>
:运行该应用程序我会收到错误:
unduffecrueld SyntaxError:未找到import:default
在生成行上:从“/src/someclass.js?t = 16555451078055”中导入someclass;
注意:
仅使用常规ES6风味类&amp; someclass.js中的导出默认myClass
效果很好,但不妨删除Typescript ...
已安装的Svelte-Preprocess
npm install -d svelte-d svelte-preprocess
更新vite> vite 。
import { defineConfig } from 'vite'
import { svelte } from '@sveltejs/vite-plugin-svelte'
import sveltePreprocess from 'svelte-preprocess' // <--- added this
// https://vitejs.dev/config/
export default defineConfig({
plugins: [svelte({
preprocess: sveltePreprocess() // <--- added this
})]
})
I have a SomeClass.ts
like:
class SomeClass {
constructor () {
console.log('created someclass')
}
}
export default SomeClass
Which compiles to SomeClass.js
'use strict'
exports.__esModule = true
const SomeClass = /** @class */ (function () {
function SomeClass () {
console.log('created someclass')
}
return SomeClass
}())
exports.default = SomeClass
And is imported into the script tag of App.svelte
like:
<script>
import SomeClass from "./SomeClass";
const instance = new SomeClass();
</script>
But when running the app I get error:
Uncaught SyntaxError: import not found: default
on generated line: import SomeClass from "/src/SomeClass.js?t=1655451078055";
Note:
Just using regular ES6 flavour class & export default MyClass
in SomeClass.js works fine, but might as well just remove Typescript...
Installed svelte-preprocess
npm install -D svelte-preprocess
Updated vite.config.js
like so:
import { defineConfig } from 'vite'
import { svelte } from '@sveltejs/vite-plugin-svelte'
import sveltePreprocess from 'svelte-preprocess' // <--- added this
// https://vitejs.dev/config/
export default defineConfig({
plugins: [svelte({
preprocess: sveltePreprocess() // <--- added this
})]
})
Deleted the compiled .js and now just work with the Typescript file directly no problems!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
该类是用与Svelte(ES模块)期望的模块系统(commonJ)编译的。
我不建议您单独编译课程,而是建议使用
并通过使用Svelte-Preprecess
&lt; script lang =“ ts”&gt;
tag直接在组件中的输入源。然后,应该与组件代码一起编译该类。如果其他任何地方都没有使用类,则可能还可以将
模块
选项更改为“ esnext”
在相应的tsconfig.json
中。The class is compiled with a different module system (CommonJS) than what is expected by Svelte (ES module).
I would not recommend compiling the class separately but instead to use
svelte-preprocess
and import/use the TypeScript source directly in the component, by using a<script lang="ts">
tag. Then the class should be compiled alongside the component code.If the class is not used anywhere else you could probably also change the
module
option to"esnext"
in the correspondingtsconfig.json
.