我有这个 JavaScript 类:
'use strict;'
/* global conf */
var properties = {
'PROPERTIES': {
'CHANNEL': 'sport',
'VIEW_ELEMENTS': {
'LOADER_CLASS': '.loader',
'SPLASH_CLASS': '.splash'
}
}
};
在 JavaScript 中我可以使用这些属性:properties.PROPERTIES.CHANNEL
是否可以将其转换为 DART?是否有最佳实践可以做到这一点?
有不同的方式。
你可以只创建一个地图
my_config.dart
const Map properties = const {
'CHANNEL': 'sport',
'VIEW_ELEMENTS': const {
'LOADER_CLASS': '.loader',
'SPLASH_CLASS': '.splash'
}
}
然后像这样使用它
main.dart
import 'my_config.dart';
main() {
print(properties['VIEW_ELEMENTS']['SPLASH_CLASS']);
}
或者您可以使用类来获得正确的自动完成和类型检查
my_config.dart
const properties = const Properties('sport', const ViewElements('.loader', '.splash'));
class Properties {
final String channel;
final ViewElements viewElements;
const Properties(this.channel, this.viewElements;
}
class ViewElements {
final String loaderClass;
final String splashClass;
const ViewElements(this.loaderClass, this.splashClass);
}
main.dart
import 'my_config.dart';
main() {
print(properties.viewElements.splashClass);
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)