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

打字稿导入错误的角度

发布时间:2020-12-17 07:01:46 所属栏目:安全 来源:网络整理
导读:我使用如下语法在我的角度1应用程序(在打字稿中)导入角度 import * as angular from 'angular'; 这会从角度模拟而不是角度导入角度,因为我的ILogService实现失败 ERROR in ./app/shared/Logger.factory.ts (38,7): error TS2420: Class ‘Logger’ incorrect
我使用如下语法在我的角度1应用程序(在打字稿中)导入角度

import * as angular from 'angular';

这会从角度模拟而不是角度导入角度,因为我的ILogService实现失败

ERROR in ./app/shared/Logger.factory.ts (38,7): error TS2420: Class
‘Logger’ incorrectly implements interface ‘ILogService’. Types of
property ‘debug’ are incompatible.
Type ‘(…argument: any[]) => void’ is not assignable to type ‘ILogCall’.

即使我尝试从vscode导航到’angular’,我也会导航到角度模拟角度定义.它应该导航到角度而不是模拟库……

如何避免这个问题?

编辑

以下是实施

我的实现是自定义服务,关于哪些打字稿在编译期间出错(错误被粘贴在上面)

class Logger implements ng.ILogService {
     info(message:string) { //some custom logic in info method}
}

angular.service('logger',Logger)

解决方法

这是因为angular-mock的打字是对角度的增强.换句话说,一个真正的ILogCall接口应该是:

interface ILogCall {
    (...args: any[]): void;   //from @typesangularindex.d.ts
    logs: string[];    //from @typesangular-mocksindex.d.ts
}

所以在你的代码中:

info(message:string) { //some custom logic in info method}

你需要返回一个正确的ILogCall对象(包含所有成员). @TheChetan和@Natarajan Nagarajugari的回答可能会有所帮助,但我不确定它是否会破坏你的TypeScript写作单元测试.

我的解决方案是在info(message:string)中:

info(message:string)
{
    var logCall: any = (...args: any[]) => {};
    logCall.logs = //something;

    return logCall as ng.ILogCall;
}

(编辑:李大同)

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

    推荐文章
      热点阅读