polymer
polymer copied to clipboard
How can I display a polymer element using <dom-module> in html with PolymerClass in js file?
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?
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);
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.