首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从web-component获取数据,该web-component包含在另一个web-component中。聚合物

从web-component获取数据,该web-component包含在另一个web-component中。聚合物
EN

Stack Overflow用户
提问于 2018-02-15 21:22:57
回答 1查看 337关注 0票数 0

我想做一个从请求中获取数据的web组件。所以,我已经这样做了:它在IndexedDB中进行请求保存,它工作得很好。

代码语言:javascript
复制
    <iron-ajax id="request"
               method="GET"
               headers='{"X-Requested-With": "XMLHttpRequest"}'
               verbose="true"
               url="https://jsonplaceholder.typicode.com/posts/{{token}}"
               handle-as="json"
               last-response="{{newData}}"
    ></iron-ajax>
    <app-indexeddb-mirror
            id="AppIndexedDBMirror"
            key="{{token}}"
            data="{{newData}}"
            persisted-data="{{lastData}}"
            log="true"
    >
    </app-indexeddb-mirror>

</template>
<script>
    class GetConfig extends Polymer.Element {
        static get is() { return 'get-config'; }
        static get properties() {
            return {
                token: {
                    type: String,
                    value: "",
                },
                lastData:{
                    type:Object,
                    value:"",
                    notify:true,
                },
                newData:{
                    type:Object,
                    value:""
                },

            }
        }
        ready() {
            super.ready();
            this.$.request.generateRequest();
        }

    }

    window.customElements.define(GetConfig.is, GetConfig);

</script>

接下来,我想将此组件包含在另一个组件中:

代码语言:javascript
复制
<dom-module id="my-view1">
  <template>
    <style include="shared-styles">
      :host {
        display: block;

        padding: 10px;
      }
    </style>

    <get-config id="gc" token="5"></get-config>
    <input id="inp" value="">
    <button on-click="getConfiguration">Press</button>
  </template>
  <script>
    class MyView1 extends Polymer.Element {
      static get is() { return 'my-view1'; }
        constructor(){
          super();
        }
        getConfiguration(){
            this.$.inp.value=this.$.gc.lastData.title;
        }
    }
    window.customElements.define(MyView1.is, MyView1);

当然,如果我按下按钮,它会产生我想要的东西,但我需要让它自动完成。我的意思是,当页面加载时,应该这样做。该怎么做呢?

附注:我知道如何在一个组件中实现这一点,但我想在两个不同的组件中实现。

EN

回答 1

Stack Overflow用户

发布于 2018-02-16 01:06:03

实际上,这是一种非常简单的聚合物方法。关于这一点,请阅读data system在聚合物。下面的内容可能会满足你的需求。my-view1

代码语言:javascript
复制
...
<get-config id="gc" token="5" last-data="{{lastData}}"></get-config>
   <!--Here you do not need button or js code to bind value between your custome component and input value. Here will be automatically will done upon your component loaded.--> 
   <iron-input bind-value="{{lastData.title}}"> 
    <input id="inp" value="{{lastData.title}}" /> 
   <iron-input>
...

在您的get-config组件中,添加lastDatanewData值作为value:""意味着它是一个String值。删除value:""或仅删除value() {return {};}这将导致空对象。(这里不是很重要,但只需得到通知)还可以向项目添加iron-input元素,如下所示:<link rel="import" href="bower_components/iron-input/iron-input.html"> DEMO nesciunt quas odio :))

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

https://stackoverflow.com/questions/48808335

复制
相关文章

相似问题

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