2016-08-01 37 views
1

我正在使用Intellij編寫angular2 dart應用程序。使用飛鏢爲angular2組件定義提供者

我創建了一個名爲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被complaning關於提供商陣列與所述錯誤消息arguments of constant creation must be constant expressions

我使用以下代碼中定義的驗證服務。

我是新來的飛鏢......但如果組件配置需要consts,我該如何提供在那裏使用的類?

感謝

回答

3

只是增加const應該做的:

providers: const [Auth] 

你看到這個錯誤是因爲[Auth]創建一個列表 - 雖然它僅包含一個const memeber - 本身就是不恆定。 (例如,它可以添加到或清除。)Dart要求您明確指定List是常量。