简体   繁体   English

“dart:html”和“dart:dom”包之间有什么区别?

[英]What's the difference between “dart:html” and “dart:dom” package?

I'm starting with some of the Dart examples. 我从一些Dart例子开始。 Then I wanted to query the DOM with document.query('#someId') as described here , but it seems there was no query method in document. 然后,我想查询与DOM document.query('#someId')所描述的在这里 ,但似乎已在文件中没有查询方法。 Also creating a new element by `new Element.tag('p') doesn't work. 通过`new Element.tag('p')创建一个新元素也行不通。

Then I figure out that it will work, when I change the imported package from dart:dom to dart:html . 然后我发现当我将导入的包从dart:dom更改为dart:html时它会起作用。 But using both of them gives me a bunch of duplicate definition of _XYZ . 但是使用它们都给了我一堆duplicate definition of _XYZ

So I wonder: 所以我想知道:

  1. what's the difference between dart:html and dart:dom package dart:htmldart:dom包之间的区别是什么
  2. which one should I use 我应该使用哪一个
  3. why can't I use both 为什么我不能同时使用两者

Answering a bit out of order 回答有点乱

  1. Which one should I use : you should use dart:html it provides the cleanest abstraction on top of the DOM. 应该使用哪一个 :你应该使用dart:html它在DOM之上提供最干净的抽象。

  2. why cant I use both : it should strictly not be needed, but you can actually get to the underlying dart:dom implementations from dart:html using a dirty hack described here . 为什么我不能同时使用它 :它应该严格不需要,但你实际上可以使用这里描述的脏黑客从dart:html实现底层dart:dom实现。 The better and more clean solution is to use Dart's ability to rename imports ie #import('dart:dom', prefix: 'dom'); 更好更干净的解决方案是使用Dart重命名导入的功能,即#import('dart:dom', prefix: 'dom'); as described by @munificent below. 正如下面的@munificent所描述的那样。

  3. whats the different between dart:html and dart:dom package . 什么是dart:html和dart:dom包之间的差异 I tend to think of the difference between them as similar to JQuery ( dart:html ) vs JS DOM manipulation ( dart:dom ). 我倾向于认为它们之间的区别与JQuery( dart:html )和JS DOM操作( dart:dom )类似。

The Dart team is trying hard to make the dart:html API as easy and unsurprising (in the good way) to use as we have gotten used to from libraries such as JQuery (dom manipulation), Tree.js (WebGL programming) and D3 (SVG drawing). Dart团队正在努力使dart:html API变得简单且不令人惊讶(以良好的方式)使用,因为我们已经习惯了来自JQuery(dom manip ), Tree.js (WebGL编程)和D3等库。 (SVG图)。 Further they also try to follow one API style across all these areas of functionality so that the SVG or WebGL API use similar constructs as the DOM API, thus ensuring that all the parts play nicely together. 此外,他们还尝试在所有这些功能区域中遵循一种API样式,以便SVG或WebGL API使用与DOM API类似的构造,从而确保所有部分可以很好地协同工作。

Update : as of may 2012 dart:dom is now deprecated and will be removed. 更新 :截至2012年5月dart:dom现已弃用 ,将被删除。

Lars did an excellent job with your question. Lars在你的问题上表现出色。 I'll just add to: 我只想补充一下:

why cant I use both 为什么我不能同时使用它们

You can, actually. 你可以,实际上。 The problem is that both libraries use the same names for a few things (mainly window ). 问题是两个库都为一些东西(主要是window )使用相同的名称。 When you import both of them, those names collide, which Dart doesn't allow. 当您导入它们时,这些名称会发生​​碰撞,Dart不允许这些名称。 To resolve that, you can import one of them with a prefix: 要解决此问题,您可以使用前缀导入其中一个:

#import('dart:html');
#import('dart:dom', prefix: 'dom');

Then, when you refer to a name imported from dart:html , you just use the name. 然后,当您引用从dart:html导入的名称时,您只需使用该名称。 When you want a DOM one, you prefix it: 当你想要一个DOM时,你的前缀是:

window     // dart:html window
dom.window // dart:dom window

An update to all the answers, and probably makes this question no longer applicable is that dart:dom has been deprecated. 所有答案的更新,可能使这个问题不再适用,dart:dom已被弃用。

See this post on dartlang website. 请参阅dartlang网站上的这篇文章

The short answer to add to the excellent answers already given: Use dart:html whenever you can. 简短的回答添加到已经给出的优秀答案:尽可能使用dart:html。 Use dart:dom when you must (and enter a bug if you have to). 必要时使用dart:dom(如果必须,请输入错误)。

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

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