javascriptpolymerpaper-elements

Polymer 3 Element - NotSupportedError


I've started the Polymer 3.0 Getting started tutorial and decided to play with it a bit. I have started a new project and I'm trying to add a paper-dropdown-menu to view1 that grabs data from an array.

view1.js:

/**
 * @license
 * Copyright (c) 2016 The Polymer Project Authors. All rights reserved.
 * This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
 * The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
 * The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
 * Code distributed by Google as part of the polymer project is also
 * subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
 */

import { PolymerElement, html } from '@polymer/polymer/polymer-element.js';
import './shared-styles.js';
import './car-make.js'

class MyView1 extends PolymerElement {
  static get template() {
    return html`
      <style include="shared-styles">
        :host {
          display: block;

          padding: 10px;
        }
      </style>

      <div class="card">
        <div class="circle">1</div>
        <h1>View One</h1>
        <car-make></car-make>
      </div>
    `;
  }
}

window.customElements.define('my-view1', MyView1);

I have created an element that will do that, and when I ran the demo it worked just fine

car-make.js element:

import {html, PolymerElement} from '@polymer/polymer/polymer-element.js';
import {} from '@polymer/polymer/lib/elements/dom-repeat.js';
import '@polymer/paper-dropdown-menu/paper-dropdown-menu.js';
import '@polymer/paper-item/paper-item.js';
import '@polymer/paper-listbox/paper-listbox.js';
/**
 * `car-make`
 * Car manufacturers dropdown
 *
 * @customElement
 * @polymer
 * @demo demo/index.html
 */


class CarMake extends PolymerElement {
  static get template() {
    return html`
      <style>
        :host {
          display: block;
        }
      </style>
      <paper-dropdown-menu label="Car manufacturer">
        <paper-listbox slot="dropdown-content" required>
        <template is="dom-repeat" items="{{cars}}">
            <paper-item>{{item}}</paper-item>
        </template>
        </paper-listbox>
      </paper-dropdown-menu>
    `;
  }
  static get properties() {
    return {
      cars: {
        type: Array,
        value: getCars(),
      }
    };
  }
  constructor() {
    super();
  }
}
function getCars() {
  return ['AC', 'ALPINA', 'Abarth'];
}
window.customElements.define('car-make', CarMake);

But it always fails with NotSupportedError: Operation is not supported triggered by polymer-fn.js at line 49 when I add it to my-view1.js:

customElements.define(klass.is,
    /** @type {!HTMLElement} */
    klass);
    return klass

This is the function that triggers the error. What am I doing wrong?


Solution

  • Defining cars value under properties style is wrong, instead use:

    static get properties() {
        return {
          cars: {
            type: Array
    
          }
        };
      }
      constructor() {
        super();
        this.cars=['AC', 'ALPINA', 'Abarth' ];
      }
    

    Here Demo (it's working but may give an error for extra dependencies. (just to inspare it)