简体   繁体   English

如何在Polymer 2.0中更改自定义元素的属性?

[英]How to change the properties of a custom element in Polymer 2.0?

I'm trying to add paper-tabs custom element to my Polymer 2.0 app. 我正在尝试向我的Polymer 2.0应用添加纸制标签自定义元素。

I'm having troubles changing the properties of the element. 我在更改元素的属性时遇到麻烦。 I want the first tab to be selected, so I'm trying to do it with this property 我希望选择第一个标签,所以我正在尝试使用此属性

But when I added that "static get properties" script, the element just disappeared from the page. 但是,当我添加该“静态获取属性”脚本时,该元素刚刚从页面中消失了。

What am I doing wrong? 我究竟做错了什么?

Code

   <paper-tabs selected="{{selected}}">
      <paper-tab>tab1</paper-tab>
      <paper-tab>tab2</paper-tab>
      <paper-tab>tab3</paper-tab>
    </paper-tabs>

    <iron-pages selected="{{selected}}">
      <div>1</div>
      <div>2</div>
      <div>3</div>
    </iron-pages>

    class MyTabs extends Polymer.Element {
      static get is() { return 'my-tabs'; },

      static get properties() {
        return {
          selected: {
            value: 0,
          }, 
        },
      },
    }

I don't know exactly where your issue can be created. 我不知道可以在哪里创建您的问题。

But you have to remove the comma after the static properties block, after the return block and your selected property need to have a "type" property that you are missing. 但是您必须在静态属性块之后,返回块和您选择的属性之后必须删除缺少的“ type”属性,然后删除逗号。

Below this is my test element I created that worked : 下面是我创建的有效的测试元素:

<link rel="import" href="bower_components/polymer/polymer-element.html">
<link rel="import" href="bower_components/paper-tabs/paper-tabs.html">
<link rel="import" href="bower_components/paper-tabs/paper-tab.html">
<link rel="import" href="bower_components/iron-pages/iron-pages.html">

<dom-module id="os-test">
    <template>
        <paper-tabs id="tabs" selected="{{tabSelected}}">
            <paper-tab>Tab 1</paper-tab>
            <paper-tab>Tab 2</paper-tab>
            <paper-tab>Tab 3</paper-tab>
        </paper-tabs>

        <iron-pages selected="{{tabSelected}}">
            <div>1</div>
            <div>2</div>
            <div>3</div>
        </iron-pages>
    </template>
    <script>
        class OsTestElement extends Polymer.Element {
            static get is() {
                return 'os-test';
            }

            static get properties() {
                return {
                    tabSelected: {
                        type: Number,
                        value: 0,
                        observer: "log"
                    }
                }
            }

            log(tabID) {
                console.log("selected tab : " + tabID);
            }
        }
        window.customElements.define(OsTestElement.is, OsTestElement);
    </script>
</dom-module>

The selected attribute will change every time you clicked on the tab. 每次单击选项卡时,所选属性都会更改。 You can check the console to see the tab selected. 您可以检查控制台以查看选择的选项卡。

If that doesn't help you, can you add more details. 如果那对您没有帮助,是否可以添加更多详细信息。

Thanks 谢谢

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

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