React-TypeScript

作者: oNexiaoyao | 来源:发表于2017-09-12 21:16 被阅读192次

使用TypeScript参考资料

使用教程

1. 全局安装React脚手架:npm install -g create-react-app 

由于代码都是经过webpack打包的,所以直接使用src="./xxx.jpg"是不能请求到相关数据的

只有通过require('./xxx.jpg') webpack才知道把图片也一起打包,才能知道资源

class SvgPage extends React.Component<RouteComponentProps<any>, {}> {
       constructor() {
       super();
   }
render() {
          return(
            <iframe src={require(`../../../static/svg/${this.props.match.params.id}.svg`)}  className="svgifr"/>
          );
    }
}

根据获取的参数(比如路由传参)动态加载静态资源

import * as React from 'react';
import { RouteComponentProps } from 'react-router-dom';
class SvgPage extends React.Component<RouteComponentProps<any>, {}> {
  constructor() {
     super();
    }
  render() {
      let _flag: boolean = true;
      try {
        require(`../../../static/svg/${this.props.match.params.id}.svg`);
      } catch (err) {
        _flag = false;
      }
      return _flag
       ? (<iframe src={require(`../../../static/svg/${this.props.match.params.id}.svg`)}  className="svgifr"/>)
       : (<iframe src={require(`../../../static/images/404.png`)}  className="svgifr"/>) ;
    }
}
export default SvgPage;

相关文章

  • React-TypeScript

    使用TypeScript参考资料 使用教程 由于代码都是经过webpack打包的,所以直接使用src="./xxx...

  • react-typescript入门

    一、项目构建 如果没有create-react-app命令行脚手架,可以先下载个 然后我们使用其创建一个types...

  • 使用React-Typescript的一个小问题

    问题 使用 创建一个React-Typescript工程之后,在使用 会出现以下报错: 解决 重新安装@types...

网友评论

    本文标题:React-TypeScript

    本文链接:https://www.haomeiwen.com/subject/vtshsxtx.html