- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我尝试在 Polymer 中使用计算属性,但总是得到一个空值。在本例中,我的自定义元素中有一个名为 datagraph 的属性。我向服务器发出 post 请求,获取 JSON 文件,计算结果,然后显示它。这是我的自定义元素:
<dom-module id="cost-card">
<template>
<style>
p.datos3{
color: #10cebc;
text-align: center;
font-size: 22px;
margin-top: 0px;
}
</style>
<p class="datos3">{{datagraph}}</p>
</template>
<script>
Polymer({
is: "cost-card",
properties:{
usr:{
type: Number,
value: 2
},
command:{
type: String,
value: "SP_GetCostoCampania"
},
datagraph:{
type: Number,
computed: 'getCost(command,usr)'
}
},
getCost: function(command,usr){
var options = {
hostname: 'localhost',
path: '/',
port: 8081,
secure: false,
method: 'POST',
headers: {
'x-powered-by': 'HTTPClient.js'
},
'Content-Type': 'application/json'
}
var innerCost;
var example1 = new HTTPClient(options);
example1.post("/executeGraph1?command="+ command + "¶m1=" + usr, function (err, res, body) {
body = JSON.parse(body);
innerCost = body[0].price * body[0].exchengeRate;
});
return innerCost;
}
});
</script>
</dom-module>
我有一个正在运行的 Express 服务器,信息正在正确传递,但 {{datagraph}} 标记保持为空。我认为这可能是因为 post 请求是一个异步任务,并且值是稍后交付的,但我也尝试过使用 Promise 得到相同的结果。
有谁知道正确的方法吗?
最佳答案
正如您所暗示的,getCost
始终会返回 undefined,因为 return innerCost
将在帖子回调之前执行。
Computed properties旨在接受其他属性作为参数,并设计为同步的。如果 getCost
接受了某些参数,即使这样,您也希望使用一个在回调中直接设置 this.datagraph
的观察者。
由于您没有向 getCost
提供任何参数,因此我建议您使用 ready
callback发出 post 请求并在回调中设置 this.datagraph
。
例如:
Polymer( {
is: "cost-card",
properties: {
usr: { type: Number, value: 2 },
command: { type: String, value: "SP_GetCostoCampania" },
datagraph: Number
},
observers: [ "getCosto(command, usr)" ],
getCosto: function ( command, usr ) {
var options = {
hostname: "localhost",
path: "/",
port: 8081,
secure: false,
method: "POST",
headers: { "x-powered-by": "HTTPClient.js" },
"Content-Type": "application/json"
};
const uri = `/executeGraph1?command=${command}¶m1=${usr}`;
new HTTPClient( options ).post( uri, ( err, res, body ) => {
// values have changed, ignore result (ideally cancel the HTTP request)
if ( command !== this.command || usr !== this.usr ) return;
body = JSON.parse( body );
this.datagraph = body[ 0 ].price * body[ 0 ].exchengeRate;
} );
}
} );
关于javascript - Polymer 计算属性上的空值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46101853/
我有一个Polymer元素,并且在它的Javascript中,我试图找到它的宽度和高度,就像它在DOM中一样。我尝试了很多方法,但总是得到0。 最佳答案 ...这是对我有用的一个: 将Polymer.
如何在“polymer 3”元素中使用“polymer 2”元素?由于元素在shadow dom内部不起作用,因此以下内容不起作用。 static get template() { retur
在我的 React 项目中,我使用 ES6 模块已有一段时间了。在我的 React 组件中,我将使用 import: import {f1,f2} from "./myLib"; 在我的 polyme
我想知道是否有用于聚合物元素的CDN,因为您必须始终下载元素,并且通过CDN导入它会更方便。在Google上找不到任何内容?还有什么理由不存在它,或者仅仅是因为它太新了? 最佳答案 我现在不知道任何托
我试图在以下取自 Polymer Path 的上下文中理解主机和目标(和元素)和 Polymer Data Flow文档。 考虑下图: 现在考虑以下语句(来自同一个 documentation ):
polymer 文档具有以下两种方式绑定(bind)示例: Polymer({ is: 'custom-element', properties:
如果我想从具有聚合物的数组中创建纸张选择器,则看起来很麻烦: ... [[item
我最近安装了 nodejs,凉亭。然后,我使用以下命令安装了 polymer : npm install -g polymer 然后,我添加了 polymer 安装路径: C:\Users\\AppD
我正在尝试通过 querySelector 或等效方法获取在另一个元素中定义的自定义 Polymer 元素。我的代码如下: Polymer({ ready: function(){
我正在使用 Polymer v3 开发一个 Web 组件,并且需要在我的新组件的模板 HTML 中包含一些在旧式 Polymer 2 组件中定义的自定义元素。 由于 Polymer 3 不再支持 HT
我有两个 polymer 元件: 它们都是父元素内的子元素 ,并且每个都位于不同的文件中。 在 ,我有一个 ,里面有一些在那里通过点击事件来获取它的值。 目标是:在 另一方面,我想从具有如下 ur
任何人都可以向我指出使用 Polymer CLI 中的 Polymer 2 和 polymer-build 的教程吗?当我使用 polymer-starter-kit 中的任何示例并使用 polyme
我需要知道何时可以开始以编程方式使用我的自定义 Polymer 元素。元素还在undefined即使在我的 window.onload处理程序。是否有使用 Polymer 1.0 正确执行此操作的既定
我正在使用 Polymer Starter Kit 作为应用程序的基础。所以,这是一个路由到特定部分的单页应用程序。我的索引基本没变,你看这里https://github.com/PolymerEle
polymer 菜鸟... 我正在尝试根据 Polymer API 文档创建一个自定义元素,我的主页如下所示: Polymer
我在迁移到 Polymer 1.0 时遇到了一些问题 我的主要问题是“style-scope”和“my-element”类被应用于元素中的每个子节点。使用这样的东西时这不是问题:
图片中显示 People also search for 的部分,有一个可滚动的水平项目列表。我必须使用 polymer 做同样的事情,但找不到任何类似的东西。我已经实现了垂直列表,但不确定水平列表。
我想创建一个用户只能实例化一次的元素。 到目前为止,我能想到的最好的做法是在匿名函数中定义元素,并在发现该元素已存在时抛出错误。有没有办法让它拒绝被创建? (function(){ var sin
我有一个简单的元素,它只允许您一次选择一个本地文件,然后将所选文件显示为您可以删除的项目,如下所示: 该组件本身工作得很好,问题是我在同一页面中有另一个相同类型的组件,但在不同的父元素(和隐藏)内。如
我正在深入研究 Polymer 1.0 元素,我对计算的属性有点好奇。 例如,在 纸抽屉面板.html , … … … Polymer({ is: 'paper-
我是一名优秀的程序员,十分优秀!