Dart 如何让库从同一个包导入库?
dart页面描述了如何让dart文件(parser_test.dart)使用“导入”包:…“导入”样式从其自己的包导入文件。这似乎暗示这是一件好事——比使用相对路径更好。所示示例适用于测试中的文件,该文件看起来很特殊。但是,为什么从包中的lib导入相同的包lib文件没有意义呢。也许这是有道理的,但如果是这样的话,pub更新并不能让它变得方便Dart 如何让库从同一个包导入库?,dart,Dart,dart页面描述了如何让dart文件(parser_test.dart)使用“导入”包:…“导入”样式从其自己的包导入文件。这似乎暗示这是一件好事——比使用相对路径更好。所示示例适用于测试中的文件,该文件看起来很特殊。但是,为什么从包中的lib导入相同的包lib文件没有意义呢。也许这是有道理的,但如果是这样的话,pub更新并不能让它变得方便 foo/ /lib/ foo_lib_1.dart foo_lib_2.dart src/
foo/
/lib/
foo_lib_1.dart
foo_lib_2.dart
src/
foo_lib_1/
foo_lib_1_impl.dart
foo_lib_2/
foo_lib_2_impl.dart
假设foo_lib_2使用foo_lib_1。foo_lib_2.dart有两个选项:
- 导入“./foo_lib_1.dart”李>
- 导入“包:foo/foo_lib_1.dart”李>
foo:
path: lib
指向pubspec.yaml中foo的依赖项。库使用包样式导入从自己的包导入另一个库(不在test、bin或example中)有什么优点或缺点?这种明显的不一致有什么原因吗
在接受了下面的答案之后,我仍然没有看到它。下面是我在shell会话中看到的内容,我希望将此行为与答案相协调。任何解释都将不胜感激。我使用的是emacs而不是DartEditor,因此这里使用的是老式的命令行方法
### Show all files, one dart library file and one yaml, plus empty
### lib and test folders
user@user-thinkpad:/tmp/uml_codegen_sample$ ls -R
.:
lib pubspec.yaml test
./lib:
plusauri.dart
./test:
### Show contents of pubspec
user@user-thinkpad:/tmp/uml_codegen_sample$ cat pubspec.yaml
name: domain_model
version: 0.0.1
description: >
Auto-generated support from /home/user/plusauri/modeling/plusauri.xmi.json
dependencies:
ebisu:
path: /home/user/open_source/codegen/dart/ebisu
### Run pub install and show the changes. Note there is a soft
### link to packages from test, but not lib.
user@user-thinkpad:/tmp/uml_codegen_sample$ pub install
Resolving dependencies...
Dependencies installed!
Some packages that were installed are not compatible with your SDK version 0.4.7+5.r21658 and may not work:
- 'pathos' requires >=0.5.0+1
You may be able to resolve this by upgrading to the latest Dart SDK
or adding a version constraint to use an older version of a package.
user@user-thinkpad:/tmp/uml_codegen_sample$ ls -R
.:
lib packages pubspec.lock pubspec.yaml test
./lib:
plusauri.dart
./packages:
domain_model ebisu pathos
./test:
packages
### Note here the program does not work, and suspiciously pub
### install put no packages link under lib like it did test
user@user-thinkpad:/tmp/uml_codegen_sample$ dart lib/plusauri.dart
Unable to open file: /tmp/uml_codegen_sample/lib/packages/ebisu/ebisu_utils.dart'file:///tmp/uml_codegen_sample/lib/plusauri.dart': Error: line 5 pos 1: library handler failed
import "package:ebisu/ebisu_utils.dart" as EBISU_UTILS;
^
### Copy the same dart file to test to show that it can run there
### just fine
user@user-thinkpad:/tmp/uml_codegen_sample$ cp lib/plusauri.dart test/
user@user-thinkpad:/tmp/uml_codegen_sample$ dart test/plusauri.dart
Main for library plusauri
user@user-thinkpad:/tmp/uml_codegen_sample$
### Finally, manually create the soft link in lib, to show it will
### then run
user@user-thinkpad:/tmp/uml_codegen_sample$ ln -s ../packages lib/packages
user@user-thinkpad:/tmp/uml_codegen_sample$ dart lib/plusauri.dart
Main for library plusauri
实际上,您完全可以使用
包:foo/foo_lib_1.dart
语法导入,而无需首先更改pubspec.yaml
甚至创建pubspec.yaml
在本测试中,您可以从语言级别看到这一点:
野外的一个例子是:
我不认为以这种或那种方式写作有任何好处,只是相对路径写得稍微短一点 从项目结构的角度来看,当钻取不向用户公开的子目录时,我将使用相对路径导入
src
通常被视为特定于实现的细节,外部用户看不到这些细节,因此请使用指向您内心内容的相对路径
但是,如果您在多个目录中工作,那么您应该使用package:
imports来强化零件是独立的和可互换的概念。在lib目录本身中,您想说这两个库虽然可能相互依赖,但它们可以独立存在,并且不受其物理位置的限制
我建议永远不要在导入中使用
。/
,因为这很脆弱,如果/当您修改目录结构或部署时,可能会以奇怪的方式中断。现在很清楚了。我认为早期的图书馆之旅或文档中提到的图书馆也可以有一个主图书馆。所以,如果我有一个库可以做一件事,比如说解析一种类型的文件,我会编写代码,并通过添加一个要解析的文件的主接受参数将其转换为脚本。这就是我失败的原因——lib下没有packages文件夹(但相对导入工作正常)。也许教训是不要仅仅因为可以就将libs加倍为二进制文件。这会清除一些本应该在语言之旅中出现的东西,或者在“优雅飞镖”中,很高兴我发现了这一点。我不知道如何使用“package”从同一个项目导入,使用两个不同的路径导入同一个文件(其中一个路径添加了相对“../”),导致同一文件的两个副本在我的程序命名空间中浮动。(一个我无法在示例项目中复制的bug)。谢谢