Angular 2 + Zone.js + Common js模块:IF语句异常,即使在false情况下也执行代码 [英] Angular 2 + Zone.js + Common js module: IF statement anomaly, code is executed even in false case

查看:110
本文介绍了Angular 2 + Zone.js + Common js模块:IF语句异常,即使在false情况下也执行代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试了很多支票。这绝对是一个异常现象。我有一个Angular 2服务,它加载 @type 定义(typescript 2),后者又加载了一个commmon.js模块( visionmedia /调试)。在common.js模块中,我有一个简单的if语句,即使条件为false并且不应该执行代码也会触发错误。 Angular应用程序使用 system.js 来加载模块。

I tried a ton of checks. This definitely is an anomaly. I have an Angular 2 service that loads a @type definition (typescript 2) which in turn loads a commmon.js module (visionmedia/debug). Inside the common.js module I have a simple if statement that triggers an error even if the condition is false and code should not be executed. The Angular app uses system.js to load modules.

常规代码 if(false){console.log('此代码未执行')}
正常行为,没有任何反应

Regular Code if (false) { console.log('This code is not executed') } Normal behavior, nothing happens

异常代码: if(false){exports.humanize = require('ms'); } 它触发
错误: zone.js:101 GET http:// localhost:8002 / ms.js 404(未找到)

错误本身有效。确实找不到那个剧本。错误的是它不应该首先出现。它应该被false if条件阻止。在我看来, zone.js 以某种方式解析该指令,即使该情况为假。我该怎么做才能避免这种情况?我需要检查是否需要一个或另一个路径,这取决于在服务器上还是在前端上调用相同的脚本。

The error itself is valid. Indeed that script is not to be found. What is deeply wrong is that it should not appear in the first place. It should be blocked by the false if condition. It seems to me that zone.js somehow parses the the instruction even if the case is false. What can I do to avoid this? I need to check wheter to require one path or another depending if the same script is called on server or on frontend.

CJS模块中的更大图片:

Bigger picture inside the CJS module:

// Trying to detect if environment is node.js
// In backend (no zones) everything works as expected
// In frontend, the require('ms') statement is executed event if condition is false. 
// I checked manually if process is defined, it's not. 
// Event the basic `false` condition also fails to block code.
if (typeof process === 'undefined') { 
    exports.humanize = require('node_modules/ms/index.js');
    console.log('Browser');
} else {
    exports.humanize = require('ms'); // If I comment this code works as intended
    console.log('Node');
}


推荐答案

你使用systemjs吗?我在多行注释块中的import-statements有类似的情况。 SystemJS使用正则表达式来检测import语句。也许它使用相同的方法来检测导出语句。

Do you use systemjs? I had a similar situation with import-statements in multi-line comment-blocks. SystemJS uses a regular expression to detect import statements. Maybe it uses the same approach for detecting export statements.

这不是zone.js中的问题,因为zonejs只执行任务。任务本身在其他地方被触发。

It is not a problem within zone.js since zonejs just executes tasks. The task itself is triggered somewhere else.

编辑(回答评论):

我认为你不应该有条件地执行export-statements。

I dont think you should conditionally execute export-statements.

也许这会有所帮助:

var myExport;
var myRequired;

if (something) {
  myExport = function() {
    console.log('exported this');
  };
  myRequired = require('something');
}
else {
  myExport = function() {
    console.log('exported something else');
  };
  myRequired = require('something-else');
}

exports.myExport = myExport;
exports.someMore = myRequired;

这篇关于Angular 2 + Zone.js + Common js模块:IF语句异常,即使在false情况下也执行代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆