【问题标题】:Can you mix regular and injected parameters in a constructor in Angular Dart?你可以在 Angular Dart 的构造函数中混合常规参数和注入参数吗?
【发布时间】:2014-01-26 21:06:51
【问题描述】:

在 Angular Dart 中,你可以在类构造函数中混合常规参数和注入参数吗?当我实例化类时,我得到一个缺少参数的错误。例如:

如果你有:

class Foo {
  String b;
  Http _http;

  Foo(String this.b, Http this._http);
}

Foo foo = new Foo('beta'); 

//Error missing arguments.

我的工作

class Foo {
  String b;
  Http _http;

  Foo(Http this._http);
}

Foo foo = new Foo(); 
foo.b = 'beta';

我想要什么:

class Foo {
  String b;
  Http _http;

  Foo(String this.b) {
    //inject an instance of _http here?
  }
}

Foo foo = new Foo('beta'); 

【问题讨论】:

    标签: dependency-injection dart angular-dart


    【解决方案1】:

    您可以将注入器作为第二个参数传递,并让您的 Foo 类的构造函数从注入器中提取 HTTP
    喜欢在这个问题的答案中显示Manual injection in Dart

    【讨论】:

    • 我有一个深度嵌套的类结构,我试图避免将父注入器向下传递到类层次结构中,而是找到一种全局获取它的方法。从这个和手动注射问题,我认为这是唯一的方法。
    猜你喜欢
    • 2017-04-12
    • 2015-04-21
    • 1970-01-01
    • 1970-01-01
    • 2019-11-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-04
    相关资源
    最近更新 更多