Search code examples
bindingpolymerpolymer-2.x

dom-bind with Polymer 2.0-preview


I wanted to start to port my Polymer 1.0 application to Polymer 2.0 (preview).

I used <template is="dom-bind"> in my index.html to support data bindings. Is this also possible with Polymer 2? I tried <dom-bind> as a component, but it did not work.


Solution

  • In Polymer 2.0, make sure to wrap the <template> inside <dom-bind> as described in the upgrade guide. It's also important to include polymer.html, which imports dom-bind.html (instead of only polymer-legacy.html).

    Polymer 1.0:

    <template is="dom-bind" id="app">
      ...
    </template>
    

    Polymer 2.0:

    <dom-bind id="app">
      <template>
        ...
      </template>
    </dom-bind>
    

    const app = document.getElementById('app');
    
    app.message = 'Hello world!';
    app.counter = 0;
    
    app._onClick = function() {
      this.counter++;
    };
    <head>
      <base href="https://polygit.org/polymer+2.2.0/components/">
      <script src="webcomponentsjs/webcomponents-lite.js"></script>
      <link rel="import" href="polymer/polymer.html">
    </head>
    <body>
      <dom-bind id="app">
        <template>
          <div>[[message]]</div>
          <div>[[counter]]</div>
          <button on-click="_onClick">Increment</button>
        </template>
      </dom-bind>
    </body>