是否可以在运行时动态加载 Svelte 模板?
问题说明
我查看了 []
的文档(here),但看起来我必须在编译时import
所有可能的模板.
I have looked at the documentation for [<svelte:component>]
(here), but that looks like I would have had to import
all of the possible templates at compile time.
在 Svelte 中是否可以根据用户操作从诸如 fetch()
调用之类的东西加载任意数量的任意模板?然后往里面注入数据?
Is it possible in Svelte to load any number of arbitrary templates from something like a fetch()
call based on a user action? Then inject data into it?
如果我打算在初始加载后更新它,使用 会不会效率低下?
Would it be inefficient to use <slot>
for something like this, if I plan on updating it after the initial load?
正确答案
技术上可以从源文本创建组件 — 例如,REPL 会这样做——因为编译器并不关心它是在 Node 中运行还是在浏览器中运行.但是绝对不推荐!(这会破坏使用 Svelte 的目的,因为编译器有点大.)
It's technically possible to create a component from the source text — the REPL does it, for example — since the compiler doesn't care if it's running in Node or the browser. But it's definitely not recommended! (It would defeat the object of using Svelte, since the compiler is somewhat large.)
相反,如果您使用 Rollup(使用 experimentalDynamicImport
和 experimentalCodeSplitting
)或 webpack,您可以动态地导入组件本身:
Instead, you can dynamically import the components themselves, if you're using Rollup (with experimentalDynamicImport
and experimentalCodeSplitting
) or webpack:
<button on:click="loadChatbox()">
chat to a customer service representative
</button>
{#if ChatBox}
<svelte:component this={ChatBox}/>
{/if}
<script>
export default {
methods: {
async loadChatbox() {
const { default: Chatbox } = await import('./Chatbox.html');
this.set({ Chatbox });
}
}
};
</script>
这篇好文章是转载于:学新通技术网
- 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
- 本站站名: 学新通技术网
- 本文地址: /reply/detail/tanhcfhgge
-
YouTube API 不能在 iOS (iPhone/iPad) 工作,但在桌面浏览器工作正常?
it1352 07-30 -
iPhone,一张图像叠加到另一张图像上以创建要保存的新图像?(水印)
it1352 07-17 -
保持在后台运行的 iPhone 应用程序完全可操作
it1352 07-25 -
使用 iPhone 进行移动设备管理
it1352 07-23 -
在android同时打开手电筒和前置摄像头
it1352 09-28 -
扫描 NFC 标签时是否可以启动应用程序?
it1352 08-02 -
检查邮件是否发送成功
it1352 07-25 -
Android微调工具-删除当前选择
it1352 06-20 -
希伯来语的空格句子标记化错误
it1352 06-22 -
Android App 和三星 Galaxy S4 不兼容
it1352 07-20