Polymer - 实现数据绑定全局值的最佳方式?
Polymer - best way to implement a global value for data binding?
在聚合物 1.0 中为所有元素实现 GOBAL_URL
值的最佳方法是什么?就像在 Angular 1 中一样,您有持续的服务。
<form is="iron-form"
id="search"
method="get"
with-credentials="true"
action="{{GOBAL_URL}}/api/v1/food_search"
content-type="application/x-www-form-urlencoded; charset=UTF-8">
我会使用 iron-meta
元素来共享环境变量 ->link
然后您可以使用iron-meta-query
获取全局变量。您甚至可以在其中存储对象。
<iron-meta id="fanPages" key="fanPages" value=
'{
"fb": "https://facebook.com/your-site",
"tw": "https://twitter.com/your-site",
"inst":"https://www.instagram.com/your-site"
}'>
然后在任何其他组件中你可以通过查询得到这个变量
<iron-meta-query id="query" key="fanPages" value="{{fanPages}}"></iron-meta-query>
对于服务器 URL,我使用 localStorage
(或 <app-localstorage-document>
元素)。它看起来像:
<app-localstorage-document key="serverUrl" data="{{_serverUrl}}"></app-localstorage-document>
<iron-ajax url="[[_serverUrl]]/api/v1/food_search"></iron-ajax>
_serverUrl
属性 初始化为默认值 URL。
在聚合物 1.0 中为所有元素实现 GOBAL_URL
值的最佳方法是什么?就像在 Angular 1 中一样,您有持续的服务。
<form is="iron-form"
id="search"
method="get"
with-credentials="true"
action="{{GOBAL_URL}}/api/v1/food_search"
content-type="application/x-www-form-urlencoded; charset=UTF-8">
我会使用 iron-meta
元素来共享环境变量 ->link
然后您可以使用iron-meta-query
获取全局变量。您甚至可以在其中存储对象。
<iron-meta id="fanPages" key="fanPages" value=
'{
"fb": "https://facebook.com/your-site",
"tw": "https://twitter.com/your-site",
"inst":"https://www.instagram.com/your-site"
}'>
然后在任何其他组件中你可以通过查询得到这个变量
<iron-meta-query id="query" key="fanPages" value="{{fanPages}}"></iron-meta-query>
对于服务器 URL,我使用 localStorage
(或 <app-localstorage-document>
元素)。它看起来像:
<app-localstorage-document key="serverUrl" data="{{_serverUrl}}"></app-localstorage-document>
<iron-ajax url="[[_serverUrl]]/api/v1/food_search"></iron-ajax>
_serverUrl
属性 初始化为默认值 URL。