Does Dart have import alias?

你离开我真会死。 提交于 2019-12-10 14:27:06

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!