javascript - 将 div 中的内容连同带样式的组件导出到文本/html 文件

标签 javascript html css reactjs styled-components

这是我将 div 中的内容连同带样式的组件导出到文本/html 文件的代码:

import React, { Component } from 'react';
import styled from 'styled-components';

class App extends Component{
constructor(props) {
super(props);
}

dwnldTemplate = () => {
    var inHTML = document.getElementById('page').innerHTML;
    var link = document.createElement('a');
    link.setAttribute('download', 'temp.html');
    link.setAttribute('href', 'data:' + 'text/html'  +  ';charset=utf-8,' + encodeURIComponent(inHTML));
    document.body.appendChild(link);
    link.click(); 
    document.body.removeChild(link);
  }

render() {

const PageStyle = styled.div`
  display: flex;
  flex-wrap: wrap;
`;

const MainHeadingStyle = styled.div `
  width: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100%;
  min-height: 50px;
  font-family: ProximaNova;
  font-size: 20px;
  font-weight: 500;
  color: #222222;
  text-align: center;
  display: block;
`;

const SubHeadingStyle = styled.div`
  width: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100%;
  min-height: 50px;
  font-family: ProximaNova;
  font-size: 18px;
  font-weight: 500;
  color: #444444;
  text-align: center;
  display: block;
`;

const WrapperStyle = styled.div`
  width: 100%;
  height: 300px;
  display: flex;
  font-family: ProximaNova;
  font-size: 18px;
  font-weight: 500;
`;

const BannerImage = styled.img`
  height: 300px;
  max-width: 1200px;
  width: 100%;
  display: block;
`;


return(
  <div>
    <PageStyle id="page">
      <MainHeadingStyle>
        Hello
      </MainHeadingStyle>
      <SubHeadingStyle >
        World
      </SubHeadingStyle>
      <WrapperStyle>
        <BannerImage src="https://wallpaperstock.net/colorado-road_wallpapers_37483_1366x768.jpg" id="placedImage" alt="PlaceImage" />
      </WrapperStyle>
      <WrapperStyle>
        Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.
      </WrapperStyle>
    </PageStyle>
    <a href="#" onClick={this.dwnldTemplate}>Download Template</a>
  </div>
);
}
}
export default App;

运行代码后,一切正常组件通过样式组件正确呈现其所有 CSS 但是当我单击下载模板链接时,下载的文件没有任何 CSS 只有内容存在。它看起来像这样: Screenshot of Output file

这里是输出html文件的源代码:

<div class="sc-fcdeBU hnxoDZ">Hello</div>
<div class="sc-gmeYpB kcmIus">World</div>
<div class="sc-kZmsYB bifgZI">
<img src="data:image/jpeg;base64" id="placedImage" alt="PlaceImage" class="sc-RcBXQ gFFQMD">
</div>
<div class="sc-kZmsYB vkNaR">
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.
</div>

请为此提供一些解决方案。

最佳答案

带样式的组件将生成一个引用链接 css 表的类名。您下载了 html,但尚未下载 css 文件。

关于javascript - 将 div 中的内容连同带样式的组件导出到文本/html 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55095956/

相关文章:

javascript - 在加载时显示,而不仅仅是在选中时显示

javascript - 如何在 Angular 2 的同一组件中使用多个 ng-content?

html - 如何在 CSS 中将最大宽度和高度限制为屏幕尺寸?

javascript - react 过渡组 : how do you unmount a component after it has finished it's exit animation

css - Bootstrap datepicker 下拉菜单作为输入的 100%

javascript - 窗口大小调整后 HTML5 Canvas 被阻止

javascript - Google Maps API - 如果找不到一个地址,请尝试另一个地址

php - 更新sql表

javascript - 每次在mysql数据库中插入新记录时刷新div

css - 在 fabrik (joomla) 中显示图像?