admin管理员组文章数量:1389758
Instead of return "HTML CONTENT";
I have a separate html file and I want to import it to my js file to return the content of it but import template from '/m-chip.html";
does not work.
element.js
import {Element as PolymerElement} from '../node_modules/@polymer/polymer/polymer-element.js';
import template from '/m-chip.html';
export class Mchip extends PolymerElement{
static get template() {
return template;
}
constructor() {
super();
}
}
customElements.define("m-chip" ,Mchip)
m-chip.html
<style>
...
</style>
<div>...</div>
How can I achieve this without jquery and plain js?
Instead of return "HTML CONTENT";
I have a separate html file and I want to import it to my js file to return the content of it but import template from '/m-chip.html";
does not work.
element.js
import {Element as PolymerElement} from '../node_modules/@polymer/polymer/polymer-element.js';
import template from '/m-chip.html';
export class Mchip extends PolymerElement{
static get template() {
return template;
}
constructor() {
super();
}
}
customElements.define("m-chip" ,Mchip)
m-chip.html
<style>
...
</style>
<div>...</div>
How can I achieve this without jquery and plain js?
Share Improve this question edited Sep 25, 2017 at 22:05 tony19 139k23 gold badges277 silver badges347 bronze badges asked Sep 24, 2017 at 17:58 Nima TahmasebiNima Tahmasebi 1412 silver badges13 bronze badges3 Answers
Reset to default 3Well importing html files is not something that javascript will understand at the moment. However you can build your project with webpack and add html-loader https://github./webpack-contrib/html-loader that specify how to treat your html import inside javascript.
Actually for html where you don't need the reference adjusting, i export the html code as a string through the js module export default :
// exports a constant
export default `
<style>
...
</style>
<div>
...
</div> `
And in the polymer element i import the ES6 module eg:
import template from '../templates/template.js'
...
static get template() {
return template;
}
It's not the best solution, but for the moment to made some tests with Polymer3 and templating structuring is not so bad !
Try
import { PolymerElement, html } from '@polymer/polymer';
import template from '/m-chip.html';
...
static get template() {
return html([template]);
}
and just plain html in the template html file, that works for me.
本文标签: javascriptImport template from separate html file in polymer 3Stack Overflow
版权声明:本文标题:javascript - Import template from separate html file in polymer 3 - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744659692a2618169.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论