typescript - 如何使用 Typescript 在 vuejs 中创建功能组件

标签 typescript vuejs2

我想用 typescript 在 vuejs 中创建一个功能组件。但是,我不确定如何设置通用部分以便我可以访问我的 Prop 。到目前为止我有这个:

import Vue, { FunctionalComponentOptions } from 'vue';

export default Vue.component<FunctionalComponentOptions>('MyWrapper', {
    functional: true,
    render: (createElement, context) => {

        if (context.props['flagSet']) {
            console.log('flagset');
        }

        return createElement('div', context.data, context.children);
    }
});

我得到的 context.props 行的错误是Element implicitly has an 'any' type because type 'FunctionalComponentOptions<Record<string, any>, PropsDefinition<Record<string, any>>>' has no index signature.
我不确定如何解决。

最佳答案

使上述代码工作的正确方法是传入一个描述 props 的接口(interface)。

interface IProps {
    flagSet: string;
}

export default Vue.component<IProps>

回顾最初的问题,我不确定我为什么要尝试插入 FunctionalComponentOptions作为类型参数。我责怪深夜的编码 session 。 :)

关于typescript - 如何使用 Typescript 在 vuejs 中创建功能组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53332095/

相关文章:

angular - 捕获异常后不在开发控制台中显示 500 错误

node.js - TypeScript:使用类型化装饰器函数装饰派生类

javascript - this.$root.$emit 运行该事件两次

javascript - Vue 插件,添加获取数据的全局组件

javascript - 在 VueJS 中按数字 (0-9) 对我的 API 数据进行排序

node.js - 如何在TypeScript接口(interface)中定义mongoose_id?

Typescript:使用嵌套只读属性时,类型防护无法按预期工作

reactjs - Electron + React + Typescript + Visual Studio Code?调试?我可以在 visual studio 中调试 electron-react 应用程序吗?

vue.js - 将 Vuex 操作分解为多个文件

javascript - 根据行程容量数在 vue js 中执行动态选项下拉列表