如何让库从同一个包导入库? [英] how to have libraries import libraries from the same package?

查看:122
本文介绍了如何让库从同一个包导入库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

飞镖页 http://pub.dartlang.org/doc/#adding -a依赖关系介绍了如何使用'import'软件包使用dart文件( parser_test.dart )从自己的包导入文件:... em>进口风格。它似乎暗示这是一件好事 - 比使用相对路径更好。此示例显示的是 test 中显示为特殊的文件。但是,为什么从包中的lib导入相同的包 lib文件没有意义。

The dart page http://pub.dartlang.org/doc/#adding-a-dependency describes how you can have a dart file (parser_test.dart) import files from its own package using the 'import "package:..." style of import. It seems to imply this is a good thing - better than using relative paths. This example shown is for a file in test which appears to be special. But, then why does it not make sense for importing same package lib files from a lib in the package. Maybe it does make sense, but if so the pub update does not make it convenient.

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 有两个选项:

Assume foo_lib_2 uses foo_lib_1. There are two options for foo_lib_2.dart:


  • import../foo_lib_1.dart ;

  • importpackages:foo / foo_lib_1.dart;

  • import "../foo_lib_1.dart";
  • import "packages:foo/foo_lib_1.dart";

我的猜测是建议方法是驻留在 lib 下的任何此类导入的第一个方法。我认为这是

更新的原因似乎会自动在 bin / em>,或 ,例如 foo - > ../ lib 。但是,它不会对顶层的 foo 中的 packages 文件夹执行相同的操作。这意味着要获得第二种类型的导入(即包导入),您需要添加:

My guess is the suggested approach is the first for any such import that resides under lib. The reason I think this is pub update seems to automagically provide a soft link in the packages folder of any of bin, test, or example to foo, like foo -> ../lib. Yet, it does not do the same for the packages folder in top level foo. This means to get the second type of import (i.e. the packages import) to work you need to add:

foo:
  path: lib

添加到 foo > pubspec.yaml 。库使用套件样式导入来导入另一个库(不是在测试 bin 示例)有任何优点或缺点从自己的包?是否有明显不一致的原因?

to the dependencies of foo in the pubspec.yaml. Is there any advantage or disadvantage for a library to use the package style import to import another library (not in test, bin, or example) from its own package? Is there a reason for the apparent inconsistency?

接受以下答案后,我仍然看不到它。这是我在shell会话中看到的,我想调和这种行为与答案。任何解释赞赏。我使用emacs而不是DartEditor,因此这里的旧式命令行方法。

After accepting the answer below, I still am not seeing it. Here is what I'm seeing in a shell session and I would like to reconcile this behavior with the answer. Any explanations appreciated. I am using emacs instead of DartEditor, thus the old-school command line approach here.

### 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


推荐答案

实际上, code> package:foo / foo_lib_1.dart 语法,而不需要更改您的 pubspec.yaml ,甚至创建 pubspec.yaml 在第一位!

Actually, you can definitely import using the package:foo/foo_lib_1.dart syntax without needing to change your pubspec.yaml or even creating a pubspec.yaml in the first place!

在这个测试中,你可以看到这是一个语言层次: https://github.com/dart- lang / bleeding_edge / blob / master / dart / tests / standalone / package / packages / package1.dart

You can see that this is true from a language level, in this test: https://github.com/dart-lang/bleeding_edge/blob/master/dart/tests/standalone/package/packages/package1.dart

,其中一个例子是: https://github.com/kevmoo/hop.dart /blob/master/lib/hop_tasks.dart#L17

and an example of this in the wild is: https://github.com/kevmoo/hop.dart/blob/master/lib/hop_tasks.dart#L17

我认为没有任何好处

从项目结构的角度来看,当我钻入子目录时,我将使用相对路径导入不会暴露给用户。 src 通常被视为外部用户不可见的实现特定详细信息,因此请使用相对路径来显示您的内容。

From a project structure point of view, I would use relative path imports when I am drilling into subdirectories that are not going to be exposed to the user. src is generally seen as implementation specific details that won't be visible to external users, so use relative paths to your heart's content.

但是,如果你在多个目录下工作,那么你应该使用 package:导入来强化零件是独立的和可互换的。在lib目录本身,你想说这两个库,虽然他们可能依赖彼此,可以分开生活,不受他们的物理位置绑定。

However, if you're working within multiple directories, then you should use package: imports to reinforce the idea that the parts are stand-alone and interchangeable. Within the lib directory itself, you want to say that these two libraries, although they might rely on each other, can live separately and are not bound by their physical location.

我建议不要在导入中使用 ../ ,因为这是脆弱的,如果/当您修改目录结构或部署时,可能会以奇怪的方式破坏。

I would recommend not ever using ../ in your imports, as that is fragile and may break in strange ways if/when you modify directory structure or deploy.

这篇关于如何让库从同一个包导入库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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