如何在Polymer中使用app-localstorage-document [英] How to work with app-localstorage-document in Polymer

查看:96
本文介绍了如何在Polymer中使用app-localstorage-document的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试弄清如何在app-localstorage-document中设置和检索值.我以前曾使用过铁元元素,并像这样进行操作:

I am trying to figure out how to set and retrieve a value in the app-localstorage-document. I worked before with the iron-meta element and did it like so:

      <iron-meta id="meta" key="id" value="{{meta}}"></iron-meta>          

      Polymer({
         is: 'login-form',
         properties: {
           meta: {
            type: String,
            value: ''
           },
         },

         getValue: function() {
           this.meta = '100'
           var savedValue = this.$.meta.byKey('id');
           console.log(savedValue);
         }

      });

但是当我尝试使用app-localstorage-document类似的东西时,它只会返回:
Promise {[[PromiseStatus]]:"resolved",[[PromiseValue]]:undefined}
我找不到有关如何使用此元素的任何示例.也许有人可以在这里帮助我.

But when I try something similar with the app-localstorage-document it just returns:
Promise {[[PromiseStatus]]: "resolved", [[PromiseValue]]: undefined}
I cant find any example on how to work with this element. Maybe someone can help me out here.

      <app-localstorage-document id="meta" key="id" data="{{meta}}" storage="window.localStorage"></app-localstorage-document>

         Polymer({
          is: 'login-form',
           properties: {
             meta: {
               type: String,
               value: ''
             },
           },

           getValue: function() {
             this.$.meta.setStoredValue('id', '50');
             var savedValue = this.$.meta.getStoredValue('id');
             console.log(savedValue);
           }
         });

推荐答案

我自己仍在研究此元素.文档不是那么直接.这是我到目前为止所了解的.

I am still studying this element myself. The documentation is not all that direct. This what I understand so far.

问题更多是关于改变您对访问存储数据的看法. app-localstorage-document元素正在为您处理.

The issue is more about changing the way you think about accessing the storage data. The app-localstorage-document element is handling it for you.

<app-localstorage-document id="meta" key="id" data="{{meta}}" storage="window.localStorage"></app-localstorage-document>

属性数据"与存储键"id"同步.每当"id"更新时,分配给"data"的变量都会更新.这意味着对键"id"的更改将冒泡到变量"meta"中.

The attribute "data" is synced with the storage key of "id". Any time "id" is updated, the variable assigned to "data" is updated. This means in this changes to the key "id" will bubble up into the variable "meta".

如果您需要访问存储中的信息,则应该可以在"meta"变量中对其进行访问.

If you need to access information in storage, it should be accessible in the "meta" variable.

this.meta.name or {{meta.name}}
this.meta.id or {{meta.id}}

这意味着分配给数据"的变量应为对象类型.

This would imply that the variable assigned to "data" should be of type Object.

meta:{
        type:Object,
        value:{},
        notify:true
}

因此,如果您使用此元素,则没有理由直接访问本地存储.这就是该元素的作用.

As a result, if you are using this element, there is no reason to directly access local storage. That is what this element is for.

这篇关于如何在Polymer中使用app-localstorage-document的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆