javascript - 未捕获的 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 文件应该(而不是)预处理或什么?
为了尝试解决这个问题,我尝试过:
resolve
在汇总中将 svelte 添加到配置中- 将 svmd 节点文件夹的路径添加到
svelte
include
汇总中的配置中
两者都没有工作。以下是我现在使用的文件:
主.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"
}
}
希望有人可以帮助我。
解决方案
您提到的库似乎在捆绑发布时出现了一些错误。如果您直接引用源组件本身,您将有更好的运气
import { Button, Slider, Fab } from 'svmd/src';
也就是说,它似乎不再积极开发了。
如果你想要材料设计,你可能想看看https://sveltematerialui.com/
推荐阅读
- java - 从 Json 获取房间 ID
- typescript - 如何通过 TypeScript 中的封装来保护和数组不被更改?
- html - 如何创建一个透明的 HTML/CSS 按钮?
- python - Python 在全局变量赋值时的行为,后跟异常
- xamarin.forms - Xamarin.Forms:带有 Sharnado.presentation.forms 的选项卡不响应点击
- pycharm - IDE重启后如何保存代码折叠?
- machine-learning - 如何在 LightGBM 中实现与二进制目标具有相同行为的自定义 logloss?
- php - 使用 PHP 安全加密 MySQL 数据库中的静态客户详细信息
- php - 即使我的 php 版本是 7.2.1,在 Laravel 6 composer update 中也出现 php 版本错误
- mysql - MySQL 8.0.12:加载数据 INFILE 给出错误 1290 secure_file_priv