javascript - 是否可以将 Polymer 3 组件拆分为单独的 JavaScript、HTML 和 CSS 文件?

标签 javascript polymer polymer-3.x

我正在尝试将 Polymer 3 组件拆分为单独的文件,例如:

import { html, PolymerElement } from '@polymer/polymer/polymer-element';

export default class TestSplit extends PolymerElement {
  static get template() {
    return html`
      <style>
        p {
          color: blue;
        }
      </style>

      <p>Hello from component</p>
    `;
  }
}

customElements.define('test-split', TestSplit);

看起来像这样:

index.js:

import { PolymerElement, html } from '@polymer/polymer/polymer-element';

import css from './style.css';
import template from './template.html';

export default class TestSplit extends PolymerElement {
  static get template() {
    return html`
      <style>${css}</style>
      ${template}
    `;
  }
}

customElements.define('test-split', TestSplit);

样式.css:

p {         
  color: blue;          
}

template.html:

<p>Hello from component</p>

编辑1:

我使用相同的 template.html 和 style.css 文件尝试了以下代码:

导入测试.js:

import { html, PolymerElement } from '@polymer/polymer/polymer-element';
import CssHtmlLoader from './cssHtmlLoader';

export default class ImportTest extends PolymerElement {
  static get template() {
    let htmlTemplate = CssHtmlLoader.prototype.getHtmlTemplate('template.html');
      console.log(htmlTemplate)
      return htmlTemplate.then(function (file) {
        return html`
          <link rel="stylesheet" href="style.css"> 
          ${file}
        `;
      });  
    }
  }
}

customElements.define('import-test', ImportTest);

我从 promise 中得到了正确的文件: enter image description here

但我也收到以下错误: enter image description here

代码有什么问题吗?

最佳答案

我认为,您想要导入 css 和 html 文件的方式是不可能的。它们是简单的 css/html 文件。他们不提供任何模块来导出这些文件。

只能导入导出的 javascript 模块。

我不认为有任何方法可以在 js 中导入 css 文件:

import css from './style.css'; // css can't be imported this way.

相反,您可以提供获取这些文件的服务:

比方说,

更新:

---------------------------------------- ------------------工作副本---------------------------- --------------------------------------

template.service.js :

export class TemplateService {

  xhrCall(url) {
    return new Promise((resolve, reject) => {
      const xhr = new XMLHttpRequest();
      xhr.open("GET", url);
      xhr.onload = () => resolve(xhr.responseText);
      xhr.onerror = () => reject(xhr.statusText);
      xhr.send();
    });
  }
  // As of now css is not dynamically included
  /*
  getCssTemplate(url) {
    return this.xhrCall(url);
  }
  */
  getHtmlTemplate(url) {
    return this.xhrCall(url);
  }

}

my-view1.html :

<div class="card">
  <div class="circle">1</div>
  <h1>View One</h1>
  <p>Ut labores minimum atomorum pro. Laudem tibique ut has.</p>
  <p>Lorem ipsum dolor sit amet, per in nusquam nominavi periculis, sit elit oportere ea.Lorem ipsum dolor sit amet, per in nusquam nominavi periculis, sit elit oportere ea.Cu mei vide viris gloriatur, at populo eripuit sit.</p>
</div>

my-view.html :

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

import { TemplateService } from './services/template.service.js';

class MyView1 extends PolymerElement {

  constructor() {
    super();
    this.tpl = new TemplateService();
    this.getView();  // <---------------call getView in the constructor.
  }

  static get template() {
    return html `<style include="shared-styles">
    :host {
      display: block;
      padding: 10px;
    }
    </style>
    `;
  }

  getView() {
    let template$ = this.tpl.getHtmlTemplate('src/templates/html/my-view1.html');
    // get the promise in a var above.
    template$.then(file => {
      let wrapperDiv = document.createElement('div'); // create a node
      wrapperDiv.innerHTML = file; // push the contents

      this._attachDom(wrapperDiv); // Attach the DOM to component's custom element**
    });
  }

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

关于javascript - 是否可以将 Polymer 3 组件拆分为单独的 JavaScript、HTML 和 CSS 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53536408/

相关文章:

java - 将回调从 JavaScript 传递到 Java (Android)

javascript - 如何在没有填充选项背景颜色的情况下更改选择器背景颜色?

polymer 铁列表显示不正确

polymer - 如何使用 mixin 设计包含在 LitElement 自定义组件内的纸卡 (Polymer 3) 的样式

javascript - 用 div 包装链接组? (PHP 是一种选择)

javascript - 如何将数组添加到对象,但过滤掉每个数组元素中除一列以外的所有列?

polymer - 如何在 Polymer 中反射(reflect)从 child 到 parent 的变化

angularjs - 用于框架组件(核心元素/纸张元素)的 Polymer-Angular 双向数据绑定(bind)

javascript - 如何将 d3.js v5 模块导入 polymer 3 元素?

webpack - Polymer 3 + Webpack -> 没有 'new' 就无法调用 PolymerElement