如果我正确理解您的问题,我认为您也可以使用 Svelte 获得您想要的东西,它与 React/Vue 具有相似的目标,但在某些方面更像是组件编译器而不是框架。
官方tutorial 阅读/玩的速度非常快,几乎可以教授所有知识。
在你的情况下,你可以看看这个playground:
App.svelte - 主应用程序文件
<script>
import Main from './Main.svelte';
import Left from './Left.svelte';
import Right from './Right.svelte';
</script>
<Main>
<Left />
<Right />
</Main>
Main.svelte - <Main> 组件(<slot> 表示来自组件调用者的内容 - 在这种情况下,它包含对左右的“调用”)
<style>
...
</style>
<div class="main">
<slot></slot>
</div>
Left.svelte - <Left> 组件
<script>
import Box from './Box.svelte';
</script>
<Box title="Left">
<p>
...
</p>
</Box>
Right.svelte 本质上等于 Left.svelte
Box.svelte - <Box> 和 <Left> 和 <Right> 共同使用的内部组件
<script>
export let title = "box";
</script>
<style>
...
</style>
<div class="box">
<h3>
{title}
</h3>
<slot></slot>
</div>
(这里Left和Right共有的部分被分解成一个Box组件,但这显然不是必需的)
为了完整起见,把例子变成一个完整的程序:
main.js
import App from './App.svelte';
const app = new App({
target: document.body,
});
export default app;
package.json
{
"name": "svelte-demo",
"version": "1.0.0",
"scripts": {
"build": "rollup -c",
"dev": "rollup -c -w",
"start": "sirv public"
},
"devDependencies": {
"rollup": "^1.12.0",
"rollup-plugin-commonjs": "^10.0.0",
"rollup-plugin-livereload": "^1.0.0",
"rollup-plugin-node-resolve": "^5.2.0",
"rollup-plugin-svelte": "^5.0.3",
"rollup-plugin-terser": "^5.1.2",
"svelte": "^3.0.0"
},
"dependencies": {
"sirv-cli": "^0.4.4"
}
}
rollup.config.js:
import svelte from 'rollup-plugin-svelte';
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;
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/rollup-plugin-commonjs
resolve({
browser: true,
dedupe: importee => importee === 'svelte' || importee.startsWith('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
}
};
function serve() {
let started = false;
return {
writeBundle() {
if (!started) {
started = true;
require('child_process').spawn('npm', ['run', 'start', '--', '--dev'], {
stdio: ['ignore', 'inherit', 'inherit'],
shell: true
});
}
}
};
}
然后:
npm install
npm run dev
(请注意,这基本上是官方苗条模板使用npx degit sveltejs/template my-svelte-project 创建的内容,如登录页面所述)
好消息是 Svelte 可以编译成标准的 JavaScript 类,因此可以使用来自常规 JavaScript 的组件。从这个角度来看,它是一个无框架的框架。例如见this article