svelte:component未显示任何内容

huangapple go评论45阅读模式
英文:

svelte:component not displaying anything

问题

I'm trying to achieve a cde like in this repl:
https://svelte.dev/repl/16b375da9b39417dae837b5006799cb4?version=3.25.0

But I get an empty <div id="#printable"></div> tab on my page

<script>
    import { onMount } from 'svelte';
    let Home;
    onMount(async () => {
        console.log('loading home ', doc.templateId)
        switch (doc.templateId) {
            case '1':
                Home = await (import('$lib/template/_artist/Home.svelte')).default;
            case '2':
            case '3':
            default:
                Home = await (import('$lib/template/_expert/Home.svelte')).default;
        }
        console.log(' home loaded ', doc.templateId)
    });
</script>

<div class='flex flex-col xl:flex-row'>
    <ResumeButtons {data} />

    <div class='flex flex-grow flex-row-reverse mt-4'>
        <!--		//used to balance the growth of the buttons panel-->
    </div>

    <div id='printable' class='flex-none'>
        <svelte:component this={Home} doc={doc} on:modified={documentModified}/>
    </div>
</div>

In my console, I can see the component was loaded:

loading home  2
 home loaded  2

I don't have any idea how I could debug what's going wrong

Just to validate the problem is not from the import path. If I import both templates, then write my code like follows, it works

import Expert from '$lib/template/_expert/Home.svelte';
import Artist from '$lib/template/_artist/Home.svelte';
//...
//then use it directly without await
//...

switch (doc.templateId) {
    case '1':
        Home = Artist;
        break
    case '2':
    case '3':
    default:
        Home = Expert;
}
英文:

I'm trying to achieve a cde like in this repl:
https://svelte.dev/repl/16b375da9b39417dae837b5006799cb4?version=3.25.0

But I get an empty &lt;div id=&quot;#printable&quot;&gt;&lt;/div&gt; tab on my page

&lt;script&gt;	
	import { onMount } from &#39;svelte&#39;;
	let Home;
	onMount(async () =&gt; {
		console.log(&#39;loading home &#39;, doc.templateId)
		switch (doc.templateId) {
			case &#39;1&#39;:
				Home = await (import( &#39;$lib/template/_artist/Home.svelte&#39;)).default;
			case &#39;2&#39;:
			case &#39;3&#39;:
			default:
				Home = await (import( &#39;$lib/template/_expert/Home.svelte&#39;)).default;
		}
		console.log(&#39; home loaded &#39;, doc.templateId)
	});
&lt;/script&gt;

&lt;div class=&#39;flex flex-col xl:flex-row&#39;&gt;
	&lt;ResumeButtons {data} /&gt;

	&lt;div class=&#39;flex flex-grow flex-row-reverse mt-4&#39;&gt;
		&lt;!--		//used to balance the growth of the buttons panel--&gt;
	&lt;/div&gt;

	&lt;div id=&#39;printable&#39; class=&#39;flex-none&#39;&gt;
		&lt;svelte:component this={Home} doc={doc} on:modified={documentModified}/&gt;
	&lt;/div&gt;
&lt;/div&gt;

In my console, I can see the component was loaded:

loading home  2
 home loaded  2

I don't have any idea how I could debug what's going wrong

Just to validate the problem is not from the import path. If I import both templates, then write my code like follows, it works

import Expert from &#39;$lib/template/_expert/Home.svelte&#39;;
import Artist from &#39;$lib/template/_artist/Home.svelte&#39;;
//...
//then use it directly without await
//...

	switch (doc.templateId) {
		case &#39;1&#39;:
			Home = Artist;
			break
		case &#39;2&#39;:
		case &#39;3&#39;:
		default:
			Home = Expert;
	}

答案1

得分: 1

括号在导入语句中不正确吗?预期应为:

(await import('$lib/template/_artist/Home.svelte')).default
// 或者
await import('$lib/template/_artist/Home.svelte').then(m => m.default)
英文:

Aren't the parentheses at the imports wrong? Would expect:

(await import(&#39;$lib/template/_artist/Home.svelte&#39;)).default
// or
await import(&#39;$lib/template/_artist/Home.svelte&#39;).then(m =&gt; m.default)

huangapple
  • 本文由 发表于 2023年5月22日 18:40:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/76305353.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定