简体   繁体   中英

defining providers for an angular2 component using dart

I'm writing an angular2 dart application using Intellij.

I created a provider called Auth that should be injected to the app component.

I defined the Auth service using the following code:

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 .

I'm new to dart... but if the Component configuration needs consts, how can I provide classes to be used there ?

thanks

Just adding const should do:

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. (For example, it could be added to, or cleared.) Dart requires you to specify explicitly that the List is constant.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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