我创建了
String
类扩展并尝试在角度飞镖模板中使用新方法。但由于未找到新方法,webapp 构建失败。如何在模板中使用扩展方法?
这是字符串扩展
string_utils.dart
:
extension NullChecking on String? {
bool isNullOrEmpty() => this?.isEmpty ?? true;
bool isBlank() => this?.trim().isNullOrEmpty() ?? true;
bool isNotBlank() => !this.isBlank();
}
extension BlankChecking on String {
bool isNotBlank() => !this.isBlank();
}
这是库文件
facade.dart
:
library facade;
export 'src/string_utils.dart';
这是带有模板的组件:
import 'package:facade/facade.dart';
import 'package:ngdart/angular.dart';
@Component(
selector: 'my-app',
template: '<h1>Hello {{name}}</h1><div>Name.isBlank(): {{name.isBlank()}}</div>',
)
class AppComponent {
var name = 'Angular';
}
我尝试构建它时控制台出现错误:
[SEVERE] build_web_compilers:ddc on lib/app_component.template.ddc.module: Error compiling dartdevc module:angular_tour_of_heroes|lib/app_component.template.sound.ddc.js
packages/angular_tour_of_heroes/app_component.template.dart:51:67: Error: The method 'isBlank' isn't defined for the class 'String'.
Try correcting the name to the name of an existing method, or defining a method named 'isBlank'.
this._textBinding_5.updateText(import9.interpolate0(_ctx.name.isBlank())) /* REF:asset:angular_tour_of_heroes/lib/app_component.dart:172:190 */;
所有这些代码都可以在 github 中找到。
随着角度模板导入您的组件类,您可以使用
export
进行扩展。
import 'package:ngdart/angular.dart';
export 'package:facade/facade.dart';
@Component(
selector: 'my-app',
template: '<h1>Hello {{name}}</h1><div>Name.isBlank(): {{name.isBlank()}}</div>',
)
class AppComponent {
var name = 'Angular';
}