问题
I found myself written tedious code when importing files into dart files like the following:
import '../../constants.dart';
I'm wondering if there is any way to create an alias to specific folder like:
import '@shared/constants.dart';
Thanks, Javi.
回答1:
No. Dart do not have import alias.
But you have absolute imports which makes up for it:
import 'package:my_lib/shared/constants.dart
This will import the file /lib/shared/constants.dart
回答2:
Dart doesn't allow you to rename imported identifiers, but it allows you to specify an import prefix
import '../../constants.dart' as foo;
...
foo.ImportedClass foo = foo.ImportedClass();
It allows also to filter imported identifiers like
import '../../constants.dart' show foo hide bar;
See also
- https://www.dartlang.org/guides/language/language-tour#libraries-and-visibility
- What is the difference between "show" and "as" in an import statement?
来源:https://stackoverflow.com/questions/53741749/does-dart-have-import-alias