gpt4 book ai didi

dart - 以编程方式创建聚合物元素

转载 作者:行者123 更新时间:2023-12-03 03:46:09 25 4
gpt4 key购买 nike

我有一个现有的应用程序(使用canvas标签的游戏​​),我想尝试使用PolymerDart为它创建UI元素(例如设置屏幕,与游戏中角色的对话框交互之类的东西)。

我正在尝试使基本点击计数器示例正常工作。我的index.html看起来像:

<html>
<head>
<title>PraxisClient</title>
<script async type="application/dart" src="app.dart"></script>
<script async src="packages/browser/dart.js"></script>
<script src="packages/web_components/platform.js"></script>
<script src="packages/web_components/dart_support.js"></script>
<link rel="import" href="app/elements/click_counter.html">
</head>
<body>
<div id="ui">
</div>
</body>
</html>

在我的应用程序中,我尝试使用以下元素创建元素:

querySelector('#ui').children.add(new Element.tag('click-counter'));

点击计数器代码都与 https://www.dartlang.org/polymer/#data-binding上给出的示例相同

<link rel="import" href="../packages/polymer/polymer.html">
<polymer-element name="click-counter">
<template>
<button on-click="{{increment}}">Click Me</button>
<p>You clicked the button {{count}} times.</p>
</template>
<script type="application/dart" src="click_counter.dart"></script>
</polymer-element>

import 'package:polymer/polymer.dart';
import 'dart:html';

@CustomTag('click-counter')
class ClickCounterElement extends PolymerElement {
@observable int count = 0;

ClickCounterElement.created() : super.created();

void increment(Event e, var detail, Node target) {
count += 1;
}
}

我得到的错误是:

URL必须由Dart主脚本导入:click_counter.dart

但是,即使将click_counter.dart导入我的应用程序中似乎也无济于事,我只是在dom中看到该标签,而没有执行任何代码。

如果我向元素( <polymer-element noscript>)添加一个“noscript”属性,那么我会看到显示了模板(但未执行任何代码)。

最佳答案

pubspec.yaml中,聚合物变压器配置的每个入口页面都需要一个入口

transformers:
- polymer:
entry_points:
- web/index.html

缺少此配置时, pub serve应该创建一个警告。

<html>
<head>
<title>PraxisClient</title>
<!-- <script src="packages/web_components/platform.js"></script>
not necessary anymore with Polymer >= 0.14.0 -->
<script src="packages/web_components/dart_support.js"></script>
<link rel="import" href="app/elements/click_counter.html">
</head>
<body>
<div id="ui">
</div>
<!-- moved down and removed the `async` attribute
these things get modified anyway when you run `pub build`
if you want to have customized behavior you need to do it
in a custom transformer or in the build output (as far as I know) -->
<script type="application/dart" src="app.dart"></script>
<script src="packages/browser/dart.js"></script>
</body>
</html>

更改

<link rel="import" href="../packages/polymer/polymer.html">



<link rel="import" href="../../packages/polymer/polymer.html">

关于dart - 以编程方式创建聚合物元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25591386/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com