加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

angularjs – 为什么我在指令定义上得到错误TS2345?

发布时间:2020-12-17 07:22:59 所属栏目:安全 来源:网络整理
导读:我尝试将现有的角度应用程序迁移到typescript(版本1.5.3): 这是代码: 'use strict'; angular.module('x') .directive('tabsPane',TabsPane) function TabsPane(itemTabs) { return { restrict: 'E',compile: compileTabs }; function compileTabs(tElement
我尝试将现有的角度应用程序迁移到typescript(版本1.5.3):

这是代码:

'use strict';
    angular.module('x')
    .directive('tabsPane',TabsPane)

    function TabsPane(itemTabs) {

        return {
            restrict: 'E',compile: compileTabs
        };

        function compileTabs(tElement) {
            var template = createTabsTemplate(); //function which i don't include here for brevity
            tElement.append(template);
        }}

当我编译javascript我得到:

error TS2345: Argument of type ‘(itemTabs: any) => { restrict: string; compile: (tElement: any) => void; }’ is not assignable to parameter of type ‘any[]’.
Property ‘push’ is missing in type ‘(itemTabs: any) => { restrict: string; compile: (tElement: any) => void; }’.

我试着理解为什么它在这里抱怨我去了angular的typescript定义:

不知何故,打字稿暗示了这个定义

directive(name: string,inlineAnnotatedFunction: any[]): IModule;

以下定义更合适:

directive(name: string,directiveFactory: IDirectiveFactory): IModule;

我是打字稿的新手,所以我认为我做错了,但我找不到任何与谷歌相关的东西.

我从旧的javascript angularJs 1.4.9代码迁移到typescript 2.6代码时遇到了同样的问题.我正在使用@ types / angular ver 1.6.39.我添加了’any’作为参数函数的返回类型,如:
angular.module("app")
      .directive("tabsPane",function TabsPane(itemTabs): any { 
            return {
                restrict: 'E',compile: compileTabs
            };

            function compileTabs(tElement) {
                var template = createTabsTemplate();
                tElement.append(template);
            }
       });

错误消失了:)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读