javascript - connectedCallback() 破坏了 Polymer 2.x Plunk

标签 javascript polymer polymer-2.x plunker

为什么 connectedCallback() 会破坏我的 Plunk? Comment and uncomment it in this demo to see what I mean.我做错了什么?

https://plnkr.co/edit/f4X7aZYj1MHSIjPttygH?p=preview

我的演示.html
<base href="https://polygit.org/polymer+v2.0.0/shadycss+webcomponents+1.0.0/components/">
<link rel="import" href="polymer/polymer-element.html">
<link rel="import" href="paper-toggle-button/paper-toggle-button.html">


<dom-module id="my-demo">
  <template>
    <style>
      :host > * {
        margin-top: 40px;
        font-size: 18px;
      }

      button.save {
        background-color: blue;
        color: white;
        margin-left: 12px;
      }
    </style>

    <paper-toggle-button checked="{{item.alice}}">Alice</paper-toggle-button>
    <paper-toggle-button checked="{{item.bob}}">Bob</paper-toggle-button>
    <paper-toggle-button checked="{{item.charlie}}">Charlie</paper-toggle-button>
    <paper-toggle-button checked="{{item.dave}}">Dave</paper-toggle-button>

    <button on-tap="_reset">Reset</button>
    <button class="save">Save</button>

  </template>

  <script>
    class MyDemo extends Polymer.Element {
      static get is() {
        return 'my-demo';
      }
      static get properties() {
        return {
          item: {
            type: Object,
            notify: true,
            value: () => {
              return {
                alice: false,
                bob: true,
                charlie: false,
                dave: true,
              };
            },
          },
          oldItem: {
            type: Object,
            notify: true,
          },
        };
      }

      constructor() {
        super();
      }

      ready() {
        super.ready();
        this.set('active', false);
        this.set('oldItem', this.item);
        //console.log('oldItem', this.oldItem);
      }

      // Uncomment the connectedCallback()
      // to see all the buttons go away
      // connectedCallback() {}

      static get observers() {
        return [
          '_itemChanged(item.*)',
        ];
      }

      _saveItem(o) {
        //console.log("new-item", o);
      }

      _itemChanged(newItem) {
        //console.log('newItem', newItem);
        //console.log('oldItem', this.oldItem);
        this.set('active', true);
        //console.log('active', this.active);
      }
    }

    window.customElements.define(MyDemo.is, MyDemo);
  </script>
</dom-module>

编辑:Here is a demo of the accepted answer.

最佳答案

您需要在 connectedCallback() { ... } 中调用 super.connectedCallback();,否则永远不会调用原始函数。

关于javascript - connectedCallback() 破坏了 Polymer 2.x Plunk,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46601861/

相关文章:

javascript - 将 Polymer 1.0 项目升级到 Polymer 3.0 的最佳方式

javascript - 触发全局事件 Polymer 2

html - vaadin 网格表格列宽控制与 css

javascript - 使用 Bing map 突出显示图钉

javascript - 如何使用jquery使用select dropdown获取单选按钮的值

javascript - appcelerator钛工作室未启动

javascript - 基础标题 Accordion 的内部链接?

javascript - 使用cloneNode重复Polymer中的div元素

polymer - Polymer 2.0 中的去抖动器

angularjs - polymer 的纸元素和 Angular Material