标签:def text 函数 export html res .text import end
react中,使用发送请求的方式把static文件夹中的前端可访问的静态文件读取成字符串:
import React, { Component } from ‘react‘; import { getRequestHeaders } from ‘../setupAjaxHeaders‘; import HyperDown from ‘hyperdown‘; import ‘./ReadMePage.css‘; class ReadMePage extends Component { constructor(props){ super(props);
this.state={ readmeHtml:undefined, }; } render(){ const getArtifactRequest = new Request(require(‘../static/readme.md‘), { method: ‘GET‘, headers: new Headers(getRequestHeaders(document.cookie)) }); fetch(getArtifactRequest).then((response) => { return response.text() }).then((text) => { const parser=new HyperDown; this.state.readmeHtml=parser.makeHtml(text); this.setState({ readmeHtml:parser.makeHtml(text), }); return <div id="readMeBody" dangerouslySetInnerHTML={{__html:this.state.readmeHtml}}></div>; }); return <div id="readMeBody" dangerouslySetInnerHTML={{__html:this.state.readmeHtml}}></div>; } } export default ReadMePage;
标签:def text 函数 export html res .text import end
原文地址:https://www.cnblogs.com/zealousness/p/10479495.html