如何使用 Angular 2 MockBackend 检查 RequestMethod 的方法类型

Posted

技术标签:

【中文标题】如何使用 Angular 2 MockBackend 检查 RequestMethod 的方法类型【英文标题】:How to check method type of RequestMethod using Angular 2 MockBackend 【发布时间】:2017-03-03 09:46:57 【问题描述】:

我使用 Angular 2 MockBackend 来模拟结果并设置响应取决于方法类型(Post|Get|...)。例如:

if (connection.request.url.endsWith('/api/authenticate') && connection.request.method === RequestMethod.Post) 

但我在编译时出错。例如:

[default] ...
Operator '===' cannot be applied to types 'RequestMethod.Post' and 'RequestMethod.Get'.

【问题讨论】:

【参考方案1】:

改用typeofindexOf

connection.request.url.endsWith('/api/authenticate') && [typeof connection.request.method].indexOf(typeof RequestMethod.Post) !== -1 

参考文献

Fun with javascript Native Array Functions - Modern Web

Sequences using JavaScript Array · ariya.io

【讨论】:

以上是关于如何使用 Angular 2 MockBackend 检查 RequestMethod 的方法类型的主要内容,如果未能解决你的问题,请参考以下文章

如何使用 @angular/upgrade 在 Angular 1 应用程序中使用 Angular 2 组件

如何安装和导入 angular 2 '@angular/router'?

如何使用 Angular cli 在 Angular 2 中导入电子

如何使用 NPM 安装 Angular 2?

如何使用 Mocha 测试 Angular 2?

如何在 Angular 2 中使用 DataTable