1

我正在使用 Intellij 编写一个 angular2 dart 应用程序。

我创建了一个名为的提供程序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

我是 dart 的新手......但是如果组件配置需要 consts,我如何提供要在那里使用的类?

谢谢

4

1 回答 1

3

只需添加const应该做:

providers: const [Auth]

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

于 2016-08-01T18:46:12.033 回答