如何检查模块的存在,而不引发的错误? [英] How to check for the existence of a module without an error being raised?

查看:180
本文介绍了如何检查模块的存在,而不引发的错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在角1.2, ngRoute 是一个独立的模块,使您可以使用其他的社区像路由器 ui.router 代替

我正在写一个开源模块,旨在为多个不同的路由器实现的工作。所以,我怎么可以检查哪些路由器加载或存在?

我在做我的模块在工厂里面以下,但它不工作的方式我希望它:

 如果(angular.module(ngRoute))
  //做ngRoute,具体的东西。
否则如果(angular.module(ui.router))
  //完成特定ui.router的东西。

这引起不加载任何模块错误。例如,如果应用程序使用 ui.router ,然后下面的错误引发了 ngRoute 查询:


  

未捕获的错误:[$喷油器:NOMOD]模块'ngRoute不可!
  你要么拼写错误的模块名称,或忘了加载它。如果
  注册一个模块,确保指定的依赖关系为
  第二个参数。



解决方案

我不知道而不被提出的一个错误检查的一种方式;但是,请注意,问题是,这是一个未捕获的错误,而不是被抛出一个错误。捕捉这样的错误的模式是以下

  {尝试angular.module(ngRoute)}赶上(ERR){/ *没有需要* /}

如果一个错误被抓住了,你可以尝试其他的模块,如果没有,可以使用第一。

如果你的行为将是相同的每个模块,你可以不喜欢以下,其中我们定义它会试图在第一个上市的模块名称的函数,如果抛出一个错误,请尝试下一个选项

  VAR tryModules =功能(名称){
  //接受模块名称的列表和
  //尝试加载它们,为了。  //如果没有选项依然存在,抛出一个错误。
  如果(names.length == 0){
    抛出新的错误(下称模块没有可以加载。);
  }  //尝试加载模块为m
  VAR米;
  尝试{
    M = angular.module(ngRoute)
  }赶上(ERR){
    M = NULL;
  }  //如果无法加载,请尝试其他
  //选项。如果是,将其返回。
  如果(M == NULL)回报tryModules(names.slice(1));
  否则返回米;
};tryModules([ngRoute,ui.router]);

In Angular 1.2, ngRoute is a separate module so you can use other community routers like ui.router instead.

I'm writing an open-source module that aims to work for multiple different router implementations. So how can I check which router is loaded or exists?

I'm doing the following inside a factory in my module, but it does not work the way I expect it to:

if (angular.module("ngRoute"))
  // Do ngRoute-specific stuff.
else if (angular.module("ui.router"))
  // Do ui.router-specific stuff.

It raises an error for whichever module is not loaded. For example, if the app is using ui.router, then the following error is raised for the ngRoute check:

Uncaught Error: [$injector:nomod] Module 'ngRoute' is not available! You either misspelled the module name or forgot to load it. If registering a module ensure that you specify the dependencies as the second argument.

解决方案

I am not aware of a way of checking without an error being raised; however, notice that the issue is that it was an Uncaught Error, not that an error was thrown. The pattern for catching such an error is the following.

try { angular.module("ngRoute") } catch(err) { /* failed to require */ }

If an error is caught, you can try the other module, and if not, you can use the first.

If your behavior will be the same for each module, you could do something like the following, in which we define a function which will attempt the first of the listed module names, and if an error is thrown, try the next option.

var tryModules = function(names) {
  // accepts a list of module names and
  // attempts to load them, in order.

  // if no options remain, throw an error.
  if( names.length == 0 ) {
    throw new Error("None of the modules could be loaded.");
  }

  // attempt to load the module into m
  var m;
  try {
    m = angular.module("ngRoute")
  } catch(err) {
    m = null;
  }

  // if it could not be loaded, try the rest of
  // the options. if it was, return it.
  if( m == null ) return tryModules(names.slice(1));
  else return m;
};

tryModules(["ngRoute", "ui.router"]);

这篇关于如何检查模块的存在,而不引发的错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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