简体   繁体   English

Angular Dart提供商

[英]Angular Dart providers

I want to make GET by web app on angular dart, but I have error in console when I try to open page with app: 我想通过角落飞镖上的网络应用程序进行GET,但是当我尝试使用app打开页面时,我在控制台中出错:

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

And this entrypoint 而这个切入点

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

And I have only one component from and one default component, because I use AngularDart Web App - a web app with material design component template from Web Storm: 我只有一个组件和一个默认组件,因为我使用AngularDart Web App - a web app with material design component来自Web Storm的AngularDart Web App - a web app with material design component模板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 {
  }
}

And with this service 并提供此服务

@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");
  }

}

All failings when I add http Client into service. 将http客户端添加到服务中时的所有失败。

You are providing BrowserClient but injecting a Client . 您正在提供BrowserClient但注入Client

Change your bootstrap to: 将您的bootstrap更改为:

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

I don't believe BrowserClient is marked with @Injectable() , so you might need: 我不相信BrowserClient标有@Injectable() ,因此您可能需要:

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

Or add in the bootstrap (or the Componente providers list): 或者添加引导程序(或组件providers列表):

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

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

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