首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在Dart中创建复合组件?

问如何在Dart中创建复合组件?
EN

Stack Overflow用户
提问于 2012-10-22 05:59:11
回答 2查看 601关注 0票数 1

如何在Dart中创建复合组件?

我想创建一些具有结构化输入字段的业务表单:标签、输入文本、特定于字段的错误消息的位置?所有表单都将使用数据驱动方法创建,因此需要以编程方式完成。

是否像创建DivElement的子类,然后添加应用了某些样式的组件一样简单?

我一直在使用dart编辑器来尝试构建一些可以做到这一点的类,但一直没有太多的运气。所有的样本都是独一无二的,与业务数据输入等关系不大。

EN

回答 2

Stack Overflow用户

发布于 2012-10-22 09:22:07

DivElement只是DOM div元素的包装器,所以不能直接扩展它。然而,新的网络技术正在很好地帮助你解决这个问题。看看Dart的web components library。还有一些社区UI库提供此功能。你可以找到他们的here。

票数 1
EN

Stack Overflow用户

发布于 2012-10-27 00:15:54

不幸的是,您不能从DOM中子类化元素...目前还没有。(这不是Dart问题,而是web开发的一般问题。)然而,一个名为"Web组件“的新系统正在构建中,以解决这个(和其他)问题。

Dart支持Web组件,您可以将Web组件编译成普通的HTML/JavaScript/CSS,并在现代浏览器上运行。您可以使用Web组件封装样式、结构和行为。这将为您提供所需的基本嵌套/合成。

下面是一个具有行为的表单的示例组件:

代码语言:javascript
复制
<!DOCTYPE html>
<!--
Copyright (c) 2012, the Dart project authors.  Please see the AUTHORS file
for details. All rights reserved. Use of this source code is governed by a
BSD-style license that can be found in the LICENSE file.
-->
<html lang="en">
<body>
<element name="x-todo-form" extends="div" constructor="FormComponent"
         apply-author-styles>
  <template>
    <!-- Note: <form> is to make "enter" work on Opera/IE. -->
    <form data-action="submit:addTodo">
      <input id="new-todo" placeholder="What needs to be done?" autofocus
             data-action="change:addTodo">
    </form>
  </template>
  <script type="application/dart">
  import 'model.dart';
  import 'package:web_components/web_component.dart';

  class FormComponent extends WebComponent {
    void addTodo(e) {
      e.preventDefault(); // don't submit the form
      if (_newTodo.value == '') return;
      app.todos.add(new Todo(_newTodo.value));
      _newTodo.value = '';
    }
  }
  </script>
</element>
</body>
</html>

您可以像这样使用此自定义元素:

代码语言:javascript
复制
<!DOCTYPE html>
<!--
Copyright (c) 2012, the Dart project authors.  Please see the AUTHORS file
for details. All rights reserved. Use of this source code is governed by a
BSD-style license that can be found in the LICENSE file.
-->
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <link rel="components" href="newform.html">
  <title>dart - TodoMVC</title>
</head>
<body>
  <section id="todoapp">
    <header id="header">
      <h1 class='title'>todos</h1>
      <x-todo-form></x-todo-form>
    </header>

请注意<head>部分中的<link>标记,它将组件拉入。然后注意<x-todo-form>定制标记,它是您在第一个示例中构建的定制组件。

塔达!自定义元素和复合元素!:)

open source Dart + Web Components project可用。running example of TODOMVC with Dart + Web Components也是可用的。看一部great video on Web Components。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13002606

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档