简体   繁体   English

HTML 和 Dart 导入的区别

[英]Difference of HTML and Dart imports

Using Polymer Dart 1.0.0-rc.6, is there a difference or any recommendation about html imports using <link rel ...> vs Dart's import syntax;使用Polymer Dart 1.0.0-rc.6,对于使用<link rel ...> html 导入与 Dart 的导入语法是否存在差异或任何建议; import '...' ? import '...'

If I use Dart's import , usually it will leave out a warning since I only import for the "side effect" of having the element loaded in Polymer, but not actually using it in Dart code, in which case I can suppress warnings by simply typing some class name that exists in the imported Dart Polymer element somewhere, followed by ;如果我使用 Dart 的import ,通常它会忽略一个警告,因为我只是为了在 Polymer 中加载元素的“副作用”而导入,但实际上并没有在 Dart 代码中使用它,在这种情况下,我可以通过简单地输入来抑制警告某个存在于导入的 Dart Polymer 元素中的类名,后跟; , however some dart polymer elements don't have any classes exposed, so I can't suppress the warnings in such cases, for example: 'package:polymer_elements/iron_icons.dart'; ,但是一些 dart 聚合物元素没有暴露任何类,因此在这种情况下我无法抑制警告,例如:'package:polymer_elements/iron_icons.dart'; has no exported classes which I can simply type out to suppress.没有导出的类,我可以简单地输入以抑制。

In other words, does it matter if I import the elements using dart's import mechanism, or via HTML import?换句话说,是使用 dart 的导入机制还是通过 HTML 导入来导入元素有关系吗? are there any plans by the dart team to support suppressing such warnings? Dart 团队是否有任何计划支持抑制此类警告?

Dart import import '...'; Dart 导入import '...'; is the way to go.是要走的路。 I haven't seen <link rel="import" > recommended anywhere.我还没有看到在任何地方推荐的<link rel="import" >

Another workaround is to reference an exported identifier in a Dart doc comment另一种解决方法是在 Dart 文档注释中引用导出的标识符

import 'package:polymer_elements/paper_item.dart';

/// Silence analyzer [PaperItem]
@PolymerRegister('some-element')
class SomeElement extends PolymerElement { ...

If I have style modules (HTML-only) where the *.dart file's only purpose is to be able to import the style module using Dart imports, I add some identifier like如果我有样式模块(仅 HTML),其中 *.dart 文件的唯一目的是能够使用 Dart 导入来导入样式模块,我会添加一些标识符,例如

const myStyleSilence = 0;

to be able to reference it in Dart doc comments to silence the unused import warning.能够在 Dart 文档注释中引用它以消除未使用的导入警告。

The main advantage of Dart imports is that the weird and something complicated rules to build the correct path for <link rel="import" src="..."> can be avoided (see https://www.dartlang.org/polymer-old/app-directories.html#into-a-non-dart-file-1 ). Dart 导入的主要优点是可以避免为<link rel="import" src="...">构建正确路径的奇怪且复杂的规则(参见https://www.dartlang.org/ polymer-old/app-directories.html#into-a-non-dart-file-1 )。 Refactoring support works (or should work) better with Dart imports.重构支持对 Dart 导入更有效(或应该更有效)。

I certainly hope that it will be possible to suppress unused import warnings eventually.我当然希望最终可以抑制未使用的导入警告。 A lot of work is going on to make the analyzer more configurable.正在进行大量工作以使分析器更具可配置性。 There is an issue from a member of the Polymer.dart team https://github.com/dart-lang/sdk/issues/22660 Polymer.dart 团队成员有一个问题https://github.com/dart-lang/sdk/issues/22660

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM