简体   繁体   English

在Polymer 1.0中实现全局变量

[英]Implement global variables in Polymer 1.0

I'm trying to replicate the example in the following section of the Polymer 0.5 docs, which uses the MonoState pattern to implement global variables in Polymer: 我正在尝试在Polymer 0.5文档的以下部分中复制示例,该示例使用MonoState模式在Polymer中实现全局变量:

http://www.polymer-project.org/0.5/docs/polymer/polymer.html#global http://www.polymer-project.org/0.5/docs/polymer/polymer.html#global

I'm trying to get this working using Polymer 1.0 but am not having much luck. 我正在尝试使用Polymer 1.0进行此操作,但运气不佳。

Here is my code for the app-globals and my-component custom elements. 这是我的app-globalsmy-component自定义元素的代码。 It's the same as in the above link apart from the slightly different syntax in Polymer 1.0. 除Polymer 1.0中的语法略有不同外,其他与上面的链接相同。 eg using dom-module instead of polymer-element . 例如使用dom-module而不是polymer-element

<dom-module name="app-globals">
    <script>
        (function() {
            // these variables are shared by all instances of app-globals
            var values = {
                firstName: 'John',
                lastName: 'Smith'
            }

            Polymer({
                 is: 'app-globals',
                ready: function() {
                    // make global values available on instance.
                    this.values = values;
                }
            });
        })();
    </script>
</dom-module>

<dom-module name="my-component">
    <template>
        <app-globals id="globals"></app-globals>
        <div id="firstname">{{$.globals.values.firstName}}</div>
        <div id="lastname">{{$.globals.values.lastName}}</div>
    </template>
    <script>
        Polymer({
             is: 'my-component',
              ready: function() {
                console.log('Last name: ' + this.$.globals.values.lastName);
              }
    });
    </script>
</dom-module>

The usage is simply: 用法很简单:

<my-component></my-component>

Initially I was encountering the error "Cannot read property '$' of undefined". 最初,我遇到错误“无法读取未定义的属性'$'”。 So I replaced the div content $.globals.values.firstName with this.$.globals.values.firstName (and similarly for the lastName field) in the my-component custom element. 因此,我在my-component自定义元素中将div内容$.globals.values.firstName替换为this.$.globals.values.firstName (同样,对于lastName字段)。

However the divs are not displaying the first and last names. 但是,div不显示名字和姓氏。 It looks like this.$.globals.values.firstName and this.$.globals.values.lastName are not set when the div's are rendered. 呈现div时,未设置this.$.globals.values.firstNamethis.$.globals.values.lastName

Would appreciate some help to resolve this. 希望能有帮助解决此问题。

Here is working example, Plunk 这是工作示例, Plunk

properties: {
          values: {
            type: Object,
            notify: true
          }
        },

        ready: function() {
          // make global values available on instance.
          this.values = {
            firstName: 'John',
            lastName: 'Smith'
          }
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM