【问题标题】:Angular Dart providersAngular Dart 提供者
【发布时间】:2019-09-04 19:31:49
【问题描述】:

我想在 Angular dart 上通过 Web 应用程序进行 GET,但是当我尝试使用应用程序打开页面时,控制台出现错误:

Uncaught Error: Invalid argument(s): No provider found for Client0.

还有这个入口点

void main() {
  bootstrap(AppComponent, [BrowserClient]);
}

而且我只有一个来自 Web Storm 的组件和一个默认组件,因为我使用了来自 Web Storm 的AngularDart Web App - a web app with material design component 模板:

@Component(
  selector: 'watch-counter',
  styleUrls: const [
    'package:angular_components/app_layout/layout.scss.css',
    'watch_counter_component.css'],
  templateUrl: 'watch_counter_component.html',
  directives: const [
    CORE_DIRECTIVES,
    materialDirectives,
  ],
  providers: const [UserService]
)
class WatchCounterComponent implements OnInit {

  final UserService userService;

  WatchCounterComponent(this.userService);

  int tabIndex = 0;

  void onTabChange(TabChangeEvent event) {
    tabIndex = event.newIndex;
  }

  final loginModalTabs = const<String>[
    'Login',
    'Registration'
  ];

  @override
  Future<Null> ngOnInit() async {
  }
}

有了这项服务

@Injectable()
class UserService {

  final Client http;
  UserService(this.http);

  dynamic extractData(Response resp) => JSON.decode(resp.body)['data'];

  Future login(String login, String password) async {
    print("login");
  }

  void register() {
    print("register");
  }

}

当我将 http 客户端添加到服务中时的所有失败。

【问题讨论】:

  • 这些信息不足以提供帮助。您能否提供一个更完整的失败代码示例,而不仅仅是上面的几行代码?
  • @matanlurey 谢谢,我添加了更多细节。我真的不明白什么问题

标签: javascript angular dart


【解决方案1】:

您提供的是BrowserClient,但注入的是Client。

将您的 bootstrap 更改为:

bootstrap(AppComponent, [
  provide(Client, useClass: BrowserClient),
]);

我不相信BrowserClient 标有@Injectable(),所以你可能需要:

bootstrap(AppComponent, [
  provide(Client, useFactory: () => new BrowserClient()),
]);

【讨论】:

    【解决方案2】:

    或添加引导程序(或 Componente providers 列表):

    const clientProvider = [
      ClassProvider(Client, useClass: BrowserClient),
    ];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-02-10
      • 1970-01-01
      • 1970-01-01
      • 2012-09-02
      • 2017-04-16
      • 2017-10-08
      • 2020-09-28
      相关资源
      最近更新 更多