简体   繁体   English

Svelte 组件为 Object 属性

[英]Svelte Components as Object Properties

I want to accept a component as a prop.我想接受一个组件作为道具。

import AComponent from './AComponent.svelte';

type MyType = {
  [key: string]: any; // just an example
}

type IWantToAcceptAComponent<T> = {
  component: SvelteComponentTyped<{ record: T }>;
}

const componentPropObject: IWantToAcceptAComponent<MyType> = {
  component: AComponent // error here
}
Error: Type 'typeof AComponent__SvelteComponent_' is missing the following properties from type 
'SvelteComponentTyped<{ record: MyType; }>': $set, $on, $destroy, $$prop_def, and 5 
more.ts(2740)

I figured it might be that the typedef should be:我想它可能是 typedef 应该是:

type IWantToAcceptAComponent<T> = {
  component: typeof SvelteComponentTyped<{ record: T }>;
}

But that causes even weirder errors.但这会导致更奇怪的错误。

It's fairly important that this typing is explicit, as it's supposed to be part of an exported interface for users.这种类型是显式的,这一点相当重要,因为它应该是用户导出界面的一部分。

You need to adjust your typing of IWantToAcceptAComponent .您需要调整IWantToAcceptAComponent的类型。 You can't use SvelteComponentTyped directly because that means you want the instance type, not the constructor type.您不能直接使用SvelteComponentTyped ,因为这意味着您需要实例类型,而不是构造函数类型。 But you also can't do typeof SvelteComponentType<..some generic> because that's a syntax error for TypeScript.但是你也不能做typeof SvelteComponentType<..some generic>因为这是 TypeScript 的语法错误。 You need to type the "accepts a class constructor that returns a specific instance" like this:您需要输入“接受返回特定实例的 class 构造函数”,如下所示:

type IWantToAcceptAComponent<T> = {
  component: new (...args: any) => SvelteComponentTyped<{ record: T }>;
}

Note that you need Svelte for VS Code version 105 or greater and (if you use it) svelte-check 2.0.0 or greater for this to work.请注意,您需要 Svelte for VS Code 版本 105 或更高版本,并且(如果您使用它) svelte-check 2.0.0 或更高版本才能正常工作。 There was a slight mismatch of the typings previously.之前的打字有轻微的不匹配。

If you want to use the code inside a TypeScript file, you need to activate the TypeScript plugin of the Svelte for VS Code extension.如果要使用 TypeScript 文件中的代码,则需要激活 Svelte for VS Code 扩展的 TypeScript 插件。 Else all Svelte imports are typed as a generic SvelteComponentDev without the proper types.否则,所有 Svelte 导入都被键入为没有正确类型的通用SvelteComponentDev

Even though I don't use typescript, I see no reasons why this would be possible with pure JS but not with TS.即使我不使用 typescript,我也看不出有什么理由可以使用纯 JS 而不是 TS。 I've made REPL which basically wraps two other 'sub components' passed as props.我制作了REPL ,它基本上包装了作为道具传递的另外两个“子组件”。

The structure is something like:结构类似于:

App.svelte App.svelte

<script>
  //App.svelte
  import A from "./A.svelte";
  import B from "./B.svelte";
  import Wrapper from "./Wrapper.svelte";

  let props = [  { component: A }, { component: B } ];
</script>

<Wrapper {props} />

Wrapper.svelte: Wrapper.svelte:

<script>
    //Wrapper.svelte
    export let props = [];
</script>

{#each props as prop}
    <p>
        <svelte:component this={prop.component} />
    </p>
{/each}

A.svelte: A.苗条:

   <!-- A.svelte -->
   <h2>COMP A</h2>

B.svelte: B.苗条:

    <!-- B.svelte -->    
    <h3>COMP B</h3>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM