将JS类导入苗条组件的正确方法是什么?

发布于 2025-02-08 09:06:12 字数 1674 浏览 2 评论 0原文

我有一个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!

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

孤檠 2025-02-15 09:06:12

该类是用与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 corresponding tsconfig.json.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文