何时在 Dart 中使用 part/part 与 import/export? [英] When to use part/part of versus import/export in Dart?

查看:226
本文介绍了何时在 Dart 中使用 part/part 与 import/export?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不完全理解使用库时part/part ofimport/export 之间的区别在飞镖.例如:

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 {
}

这两种情况似乎都在做同样的事情.什么时候使用partpart ofimport更有利?是否存在 import 不起作用,但 partpart of 会起作用的情况?

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?

推荐答案

update 2018/03

partpart of 最近越来越多地用于代码生成场景(而不是弃用的转换器),并且不太可能很快消失.

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_valuejson_serializable 等包依赖于它.

不鼓励的只是将一个包的所有文件绑定到一个库的模式,即让一个库文件和所有其他文件成为该库的一部分.

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 中,私有成员可以在同一个库中访问.使用 import 可以导入一个库并且只能访问它的公共成员.使用 part/part of,您可以将一个库拆分为多个文件,并且这些文件中的所有代码都可以访问私有成员.

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.

请参阅上面更新中对以下段落的说明

使用 part/part of 是不鼓励的,Dart 团队正在考虑摆脱它.我假设他们会介绍类似朋友"(https://github.com/dart-lang/sdk/issues/22841),其中两个库可以在停止 part/part of(可能在一个未来的 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 中使用 part/part 与 import/export?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆