首页 > 解决方案 > 未捕获的 TypeError:svmd_es.Button 不是在 svelte 中实现 UI 工具包的构造函数

问题描述

我是 Svelte 的新手,我在第一次实验中尝试了一个 Material Design 工具包。我找到了https://github.com/hkh12/svmd,看起来不错,我将它添加到我的 Svelte 项目中。

根据文档,没有什么比导入组件和使用它们更多的了。但是,这在浏览器中给了我以下错误:

Uncaught TypeError: svmd_es.Button is not a constructor
    create_fragment bundle.js:798
    init index.mjs:1451
    App bundle.js:873
    app main.js:4
    <anonymous> bundle.js:890

查看 Nodesvmd模块文件夹,我发现组件本身就是 .svelte 文件;既简单.svelte又编译的形式。我不明白为什么简单地导入它们并使用它们是行不通的。

我唯一能想到的是我的汇总文件中可能遗漏了一些东西?也许需要先转译一些东西,或者 node_modules 文件夹中的 .svelte 文件应该(而不是)预处理或什么?

为了尝试解决这个问题,我尝试过:

两者都没有工作。以下是我现在使用的文件:

主.js

import App from './App.svelte';

const app = new App({
    target: document.body
});

export default app;

应用程序.svelte

<script>
    import { Button, Slider, Fab } from 'svmd';
    import 'svmd/dist/svmd.css';
</script>

<main>
    <Button>flat button</Button>
    Lets see a button.
</main>

<style>
</style>

Rollup.config.js

import svelte from 'rollup-plugin-svelte';
import scss from 'rollup-plugin-scss';
import resolve from '@rollup/plugin-node-resolve';
import commonjs from '@rollup/plugin-commonjs';
import livereload from 'rollup-plugin-livereload';
import { terser } from 'rollup-plugin-terser';

const production = !process.env.ROLLUP_WATCH;

function serve() {
    let server;
    
    function toExit() {
        if (server) server.kill(0);
    }

    return {
        writeBundle() {
            if (server) return;
            server = require('child_process').spawn('npm', ['run', 'start', '--', '--dev'], {
                stdio: ['ignore', 'inherit', 'inherit'],
                shell: true
            });

            process.on('SIGTERM', toExit);
            process.on('exit', toExit);
        }
    };
}

export default {
    input: 'src/main.js',
    output: {
        sourcemap: true,
        format: 'iife',
        name: 'app',
        file: 'public/build/bundle.js'
    },
    plugins: [
        svelte({
            // enable run-time checks when not in production
            dev: !production,
            // we'll extract any component CSS out into
            // a separate file - better for performance
            css: css => {
                css.write('public/build/bundle.css');
            }
        }),

        // If you have external dependencies installed from
        // npm, you'll most likely need these plugins. In
        // some cases you'll need additional configuration -
        // consult the documentation for details:
        // https://github.com/rollup/plugins/tree/master/packages/commonjs
        scss(),
        resolve({
            browser: true,
            dedupe: ['svelte']
        }),
        commonjs(),

        // In dev mode, call `npm run start` once
        // the bundle has been generated
        !production && serve(),

        // Watch the `public` directory and refresh the
        // browser on changes when not in production
        !production && livereload('public'),

        // If we're building for production (npm run build
        // instead of npm run dev), minify
        production && terser()
    ],
    watch: {
        clearScreen: false
    }
};

包.json

{
  "name": "svelteapp",
  "version": "1.0.0",
  "scripts": {
    "build": "rollup -c",
    "dev": "rollup -c -w",
    "start": "sirv public"
  },
  "devDependencies": {
    "@rollup/plugin-commonjs": "^14.0.0",
    "@rollup/plugin-node-resolve": "^8.4.0",
    "rollup": "^2.3.4",
    "rollup-plugin-livereload": "^1.0.0",
    "rollup-plugin-scss": "^2.6.0",
    "rollup-plugin-svelte": "^5.0.3",
    "rollup-plugin-terser": "^7.0.0",
    "svelte": "^3.0.0",
    "svmd": "^0.3.1"
  },
  "dependencies": {
    "sirv-cli": "^1.0.0"
  }
}

希望有人可以帮助我。

标签: javascriptsvelterollupjssvelte-3svelte-component

解决方案


您提到的库似乎在捆绑发布时出现了一些错误。如果您直接引用源组件本身,您将有更好的运气

import { Button, Slider, Fab } from 'svmd/src';

也就是说,它似乎不再积极开发了。

如果你想要材料设计,你可能想看看https://sveltematerialui.com/


推荐阅读