我有一个提供模型对象的组件,这个模型对象可以是几种类型,它决定了哪个组件将在里面呈现:
<div [ngSwitch]="poolModel.runtimeType.toString()">
<template ngSwitchCase="CryptonoteMiningPool"><cryptonote-pool [model]="poolModel"></cryptonote-pool></template>
<template ngSwitchCase="DaggerHashimotoMiningPool"><dag-hash-pool [model]="poolModel"></dag-hash-pool></template>
</div>
这在调试模式下效果很好,但是一旦我为发布版本编译,runtimeType总是返回&#34; fS&#34;。
我有一个解决方案,基本上在模型中设置一个常量并查看它,但如果我可以避免它,我宁愿也不会有麻烦,因为我可能最终需要维护许多类型的模型。
有没有办法让runtimeType返回我在发布模式下的期望?
答案 0 :(得分:2)
运行时类型不是我用于程序逻辑的东西。它会根据编译器选项而改变,实际上即使使用它也会使你的应用程序(在dart2js中)的优化变得更加困难。
我们禁止在dart2js性能指南中使用它。
你可能最好创建某种基类:
abstract class DynamicRender {
String get renderType;
}
让你的课程扩展/混合/实现并使用它。