Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How can I display a polymer element using <dom-module> in html with PolymerClass in js file? #5657

Open
hexiaoting opened this issue May 9, 2020 · 2 comments
Labels

Comments

@hexiaoting
Copy link

I want to set element's template directly in html file , but the properties and the others are set in js file like this:

html file:

"dom-example.html" 24L, 531C                                                                                                                22,21-22      All
<html>
    <head>
  <!-- Polyfills only needed for Firefox and Edge. -->
</head>
    <body>
  <script src="https://unpkg.com/@webcomponents/webcomponentsjs@next/webcomponents-loader.js"></script>
  <script type="module" src="./dom-ele.js">
  </script>

  <dom-module id="my-element">
    <template>
      <style>
        .mood { color: green; }
      </style>
      <div>{{prop}}</div>
      <p>
        Web Components are <span class="mood">[[prop]]</span>!
      </p>
    </template>
  </dom-module>

  <my-element prop="OK"></my-element>
  </body>
  </html>

js file:

import {PolymerElement, html} from 'https://unpkg.com/@polymer/polymer@next/polymer-element.js?module';

class MyElement extends PolymerElement {
  static get properties() { return { prop: String }}
}

customElements.define('my-element', MyElement);

The result is : when access html file in chrome, nothing showed.

I‘ve tried to move the dom-module's template into js file using "static get template() {...}", that's OK. But How can I set dom-module template in html no in js?
How can I fix this?

@kevinpschaaf
Copy link
Member

As HTML Imports are deprecated as a browser feature, in Polymer 3 we no longer support authoring templates directly in HTML. There is no browser-native way to do this without a build step.

If all you want is to be able to author the HTML in html, at this point what you would need is a simple build-time transform that did something like this:

my-element.html

<!-- your template contents here -->

⬇️

my-element.html.js

import {html} from '@polymer/polymer-element.js';
export const template = html`
<!-- your template contents here -->
`;

And then in your element...

import {PolymerElement} from 'https://unpkg.com/@polymer/polymer@next/polymer-element.js?module';
import {template} from './my-element.html.js';
class MyElement extends PolymerElement {
  static get properties() { return { prop: String }}
  static get template() { return template; }
}
customElements.define('my-element', MyElement);

@stale
Copy link

stale bot commented Jun 2, 2021

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the wontfix label Jun 2, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants