简体   繁体   English

Util 模块中 util.isNullOrUndefined(object) 的替代方法是什么? Angular 7

[英]what is the alternative for the util.isNullOrUndefined(object) in the Util module ? Angular 7

util.isNullOrUndefined(object) has been depreciated and I cannot find any alternative for it. util.isNullOrUndefined(object) 已被贬值,我找不到任何替代品。 Can someone point out a feasible alternative for it?有人可以指出一个可行的替代方案吗?

if (isNullOrUndefined(this.activeAppKey) || this.activeAppKey.trim().length === 0) {
  this.activeAppKey = sessionStorage.getItem(AppConstants.APP_KEY);
}

try using !尝试使用! operator.操作员。

if (!this.activeAppKey) {
  this.activeAppKey = sessionStorage.getItem(AppConstants.APP_KEY);
}

Why not simply create you own:为什么不简单地创建你自己的:

export function isNullOrUndefined(value: any) {
    return value === null || value === undefined;
}

Lets say you create a tools.ts file and write the above code:假设您创建了一个 tools.ts 文件并编写了上述代码:

You can use it in your component as follows :您可以在您的组件中使用它,如下所示:

import { isNullOrUndefined } from 'tools';

The above code will work no changes there.上面的代码在那里没有任何变化。 Plus it will reusable另外它可以重复使用

try this:尝试这个:

if (this.activeAppKey === null || this.activeAppKey === undefined || this.activeAppKey.trim().length === 0) {
  this.activeAppKey = sessionStorage.getItem(AppConstants.APP_KEY);
}

I would go for this approach.我会采用这种方法。 Create some util class and add this as a static method.创建一些 util 类并将其添加为静态方法。

export class Util {
  static isNullOrUndefined<T>(obj: T | null | undefined): obj is null | undefined {
    return typeof obj === 'undefined' || obj === null;
  }
}

You can use the isNullOrUndefined function from the core-util-is npm package.您可以使用core-util-is npm 包中的isNullOrUndefined函数。

It was deprecated from nodejs core because behaviour or api changes on these functions could break a lot of existing code without notifying the users.它已从 nodejs 核心弃用,因为这些函数的行为或 api 更改可能会在不通知用户的情况下破坏大量现有代码。 In a npm package, using semver, only a major version bump can break api/behaviour.在 npm 包中,使用 semver,只有一个主要版本的颠簸才能破坏 api/behaviour。 A lot safer this way.这样安全多了。

import it from 'is-what'从'is-what'导入

so,所以,

import { isUndefined, isNullOrUndefined } from 'util'; import { isUndefined, isNullOrUndefined } from 'util';

will become,会变成,

import { isUndefined, isNullOrUndefined } from 'is-what'; import { isUndefined, isNullOrUndefined } from 'is-what';

From NodeJs reference [https://nodejs.org/api/util.html#utilisnullorundefinedobject] :从 NodeJs 参考 [https://nodejs.org/api/util.html#utilisnullorundefinedobject] :

Deprecated: Use value === undefined || value === null不推荐使用:使用value === undefined || value === null value === undefined || value === null instead. value === undefined || value === null代替。

This makes the answer given by super IT guy above correct ( https://stackoverflow.com/a/56138823/6757814 )这使得上面超级 IT 人员给出的答案正确( https://stackoverflow.com/a/56138823/6757814

Using a double equals on a null checks for null and undefined exclusivelynull上使用双等号检查nullundefined

if (this.activeAppKey == null || this.activeAppKey.trim().length === 0)

null == undefined // true
null == null      // true
null == 0         // false
null == ""        // false
null == false     // false

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM