在 Angular dart 模板中使用库中定义的 String 扩展方法

问题描述 投票:0回答:1

我创建了

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 中找到。

angular-dart
1个回答
0
投票

随着角度模板导入您的组件类,您可以使用

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';
}
© www.soinside.com 2019 - 2024. All rights reserved.