[英]defining providers for an angular2 component using dart
我正在使用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正在抱怨帶有錯誤消息的providers數組arguments of constant creation must be constant expressions
。
我是dart的新手...但如果組件配置需要consts,我如何提供在那里使用的類?
謝謝
只需添加const
應該:
providers: const [Auth]
您看到的錯誤是因為[Auth]
創建了一個List - 雖然它只包含一個const memeber - 但它本身並不是常數。 (例如,可以添加或清除它。)Dart要求您明確指定List是常量。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.