【问题标题】:How to use angular2_components如何使用 Angular 2 组件
【发布时间】:2017-03-23 06:00:33
【问题描述】:

谁能告诉我,如何使用 angular2_components? 我试过如下:

<material-button>Test</material-button> 

但它没有按预期显示按钮。

更新 我做了什么:

pubspec yaml:

dependencies:
  angular2: ^2.0.0
  angular2_components: ^0.1.0
dev_dependencies:
  browser: ^0.10.0
  dart_to_js_script_rewriter: ^1.0.1
transformers:
- angular2:
    platform_directives:
    - 'package:angular2/common.dart#COMMON_DIRECTIVES'
    platform_pipes:
    - 'package:angular2/common.dart#COMMON_PIPES'
    entry_points: web/main.dart

app_component.dart:

import 'package:angular2/core.dart';
import 'package:angular2_components/angular2_components.dart';

@Component(
    selector: 'my-app',
    directives: const[
      MaterialButtonComponent,
    ],
    styleUrls: const ['app_component.css'],
    templateUrl: 'app_component.html')
class AppComponent {}

按钮仍然看起来像:

并且还收到错误消息:

我做错了什么?

【问题讨论】:

  • 您是否包含了 angular2_components 所需的 js/css 文件?
  • 它不在包装内?如何获得它们?

标签: angular dart material-design


【解决方案1】:

你需要添加

dependencies:
  angular2_components: ^0.1.1

到您的pubspec.yaml 文件并运行pub get

那你需要导入MaterialButtonComponent

import 'package:angular2_components/angular2_components.dart'
    show MaterialButtonComponent, materialProviders; // the show Xxx is optional

然后将其添加到您要使用它的组件的@Directives()

@Component(
  selector: 'my-app',
  directives: const [
    MaterialButtonComponent,
  ],
  providers: [materialProviders],
  ...
)

更多详情见https://dart-lang.github.io/angular2_components_example/https://github.com/dart-lang/angular2_components_example/blob/master/lib/app_component.html

【讨论】:

  • 将指令数组显式键入为dynamic 有什么好处?还是您需要在强模式下使用它?
  • 首先,非常感谢您,但 是什么意思?
  • 这不是必需的,它只是一个泛型类型参数,专门用于列表 ([a, b, c]) &lt;Abc&gt;[] 成为 Abc 列表(如 new List&lt;Abc&gt;())或 &lt;dynamic&gt;成为一切的清单。我启用了一个 linter 规则,它在任何地方强制执行类型注释,但是根据 Dart 样式指南,不鼓励添加太多类型注释,而且它很吵。当分析仪还没有现在那么强大时,它非常方便。
  • 除了图标不需要添加CSS。对于图标添加&lt;link rel="stylesheet" type="text/css" href="https://fonts.googleapis.com/icon?family=Material+Icons"&gt;
  • 因为我遇到了同样的错误并询问 ;-) 这是一个用于涟漪效应的全局服务。
猜你喜欢
  • 2016-08-04
  • 2017-09-15
  • 1970-01-01
  • 2020-01-11
  • 1970-01-01
  • 1970-01-01
  • 2017-03-21
  • 2016-08-17
  • 2019-05-02
相关资源
最近更新 更多