【问题标题】:defining providers for an angular2 component using dart使用 dart 为 angular2 组件定义提供程序
【发布时间】:2016-12-06 22:45:30
【问题描述】:

我正在使用 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,我如何提供要在那里使用的类?

谢谢

【问题讨论】:

    标签: intellij-idea angular dart angular-components


    【解决方案1】:

    只需添加const 即可:

    providers: const [Auth]
    

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

    【讨论】:

      猜你喜欢
      • 2016-07-27
      • 2017-01-09
      • 1970-01-01
      • 2023-01-23
      • 2016-04-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多