感谢Rutuja's answer here,我也能够让我的应用程序正常工作。添加到 Rutuja 的答案中,如果有人在使用 Sapper + Svelte + 另一个包(在我的情况下为 theme.js)时遇到此问题,您可能还需要导入多个组件。这在Mozilla's documentation here for import 中有描述。
当使用动态导入导入默认导出时,它会起作用
不同。您需要解构并重命名“默认”键
返回的对象。
(async () => {
if (somethingIsTrue) {
const { default: myDefault, foo, bar } = await import('/modules/my-module.js');
}
})();
对于我自己的项目(使用theme.js),我必须使用以下逻辑(基于位于here 的theme.js 的精简示例):
const themeState = {
active: undefined,
selected: undefined,
themes: [],
}
onMount(async () => {
const { default: Themer, auto, system } = await import('themer.js')
themeState = {
active: undefined,
selected: light,
themes: [light, dark, auto, system],
}
const themer = new Themer({
debug: true,
onUpdate: (theme) => (themeState.active = theme),
themes: { light, dark, auto, system },
})
function noThemeSupport({ theme }) {
return theme === 'system' && !themer.themeSupportCheck()
}
function setTheme(theme) {
themeState.selected = theme
themer.set(theme)
}
themer.set(themeState.selected)
})