何时在Dart中使用零件/零件与导入/导出? [英] When to use part/part of versus import/export in Dart?
问题描述
我不完全理解的部分
/ 部分和
导入
/ export
在Dart中使用库时。例如:
I do not completely understand the difference between part
/part of
and import
/export
when using libraries in Dart. For example:
one.dart:
library one;
part "two.dart";
Class One {
};
和
two.dart:
part of one;
import 'somefile.dart';
Class Two {
}
对比
library one;
import 'two.dart';
Class One {
}
和
library two;
import 'somefile.dart';
export 'somefile.dart';
Class Two {
}
两种情况似乎都做同样的事情。什么时候使用的部分
和部分而不是
导入
?还有在某些情况下导入
不起作用,但是部分
和部分
会吗?
Both scenarios seem to do the same thing. When is it advantageous to use part
and part of
rather than import
? And are there scenarios where import
will not work, but part
and part of
will?
推荐答案
更新2018/03
部分
和部分
用于代码生成方案(而不是
part
and part of
is used more and more for code generation scenarios recently (instead of deprecated transformers) and unlikely to go away anytime soon.
像 built_value
, json_serializable之类的软件包
,还有许多其他依赖于此的文件。
Packages like built_value
, json_serializable
, and many others depend on it.
只有通过将软件包中的所有文件捆绑到一个库中,Discouraged才是这样一个库文件,所有其他文件都属于该库。
Discouraged is only the patter where all files of a package are tied together to a single library by having one library file and all other files being part of that library.
原始
在Dart中,可以在同一库中访问私有成员。使用导入
,您可以导入库,并且只能访问其公共成员。使用部分
/ 部分
的一部分,您可以将一个库分成几个文件,并且私有成员可访问这些文件中的所有代码。
In Dart, private members are accessible within the same library. With import
you import a library and can access only its public members. With part
/part of
you can split one library into several files and private members are accessible for all code within these files.
请参阅以上更新中对以下段落的说明
使用<$ c $建议不要使用中的c>部分 / 部分,Dart团队正在考虑摆脱它。我以为他们会介绍类似朋友( https://github.com/dart- lang / sdk / issues / 22841 ),其中两个图书馆在终止
part
/ part之前可以互访对方的私有成员。的
(可能在将来的Dart版本中)。
Using part
/ part of
is discouraged and the Dart team is considering getting rid of it. I assume they will introduce something like "friend" (https://github.com/dart-lang/sdk/issues/22841), where two libraries can access each other's private members as an alternative before they discontinue part
/ part of
(maybe in a future Dart version).
这篇关于何时在Dart中使用零件/零件与导入/导出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!