简体   繁体   English

使用dart定义angular2组件的提供程序

[英]defining providers for an angular2 component using dart

I'm writing an angular2 dart application using Intellij. 我正在使用Intellij编写angular2 dart应用程序。

I created a provider called Auth that should be injected to the app component. 我创建了一个名为Auth的提供程序,应该将其注入应用程序组件。

I defined the Auth service using the following code: 我使用以下代码定义了Auth服务:

import 'package:angular2/core.dart';
import 'package:auth0_lock/auth0_lock.dart';
import './config.dart';

@Injectable()
class Auth {
Auth0Lock lock;

Auth() {
    this.lock = new Auth0Lock(configObj.auth0.apiKey, configObj.auth0.domain);
}
updateProfileName(data) {
  var profile = data['profile'] != null ? data['profile'] : data;
  print(profile['name']);
}

login() {
    this.lock.show(popupMode: true, options: {'authParams': {'scope': 'openid profile'}}).then(this.updateProfileName);
}
}

and the app component using the following code: 和应用程序组件使用以下代码:

import 'package:angular2/core.dart';
import 'package:angular2/router.dart';
import 'welcome_component.dart';
import 'auth_service.dart';

@Component(
    selector: 'my-app',
    templateUrl: '/www/my-app.html',
   providers: [Auth]
)
@RouteConfig(const [
  const Route(path: '/welcome', component: WelcomeComponent, name: 'welcome')
])
class AppComponent {
  Auth auth;
  AppComponent(Auth auth) {
    this.auth=auth;
  }
}

now intellij is complaning about the providers array with the error message arguments of constant creation must be constant expressions . 现在intellij正在抱怨带有错误消息的providers数组arguments of constant creation must be constant expressions

I'm new to dart... but if the Component configuration needs consts, how can I provide classes to be used there ? 我是dart的新手...但如果组件配置需要consts,我如何提供在那里使用的类?

thanks 谢谢

Just adding const should do: 只需添加const应该:

providers: const [Auth]

The error you're seeing is because [Auth] creates a List that — although it contains only a const memeber — is itself not constant. 您看到的错误是因为[Auth]创建了一个List - 虽然它只包含一个const memeber - 但它本身并不是常数。 (For example, it could be added to, or cleared.) Dart requires you to specify explicitly that the List is constant. (例如,可以添加或清除它。)Dart要求您明确指定List是常量。

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

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