intellij理念-使用dart定义angular2组件的提供者



我正在使用Intellij编写angular2飞镖应用程序。

我创建了一个名为Auth的提供程序,它应该被注入到应用程序组件中。

我使用以下代码定义了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);
}
}

和应用程序组件使用以下代码:

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;
  }
}

现在intellij正在处理带有错误消息arguments of constant creation must be constant expressions的providers数组。

我是飞镖新手。。。但是,如果组件配置需要常量,我如何提供要在那里使用的类?

感谢

只需添加const即可:

providers: const [Auth]

您看到的错误是因为[Auth]创建了一个List,尽管它只包含一个常量,但它本身并不是常量。(例如,它可以被添加到或清除。)Dart要求您明确指定List是常量。

最新更新