react+ts准备工作—阿楠

安装项目

  • 安装全局yarn
  • npm i yarn -g
  • // 安装项目
  • npx create-react-app 你的项目名 --template typescript
  • 进入项目目录
  • cd 你的项目名
  • 安装需要的支持库(redux,react-router-dom)
  • yarn add redux react-redux@types/react-redux -S
  • yarn add react-router-dom @types/react-router-dom -S
  • 安装需要的第三库(antd,babel-plugin-import,axios,mockjs)
  • yarn add babel-plugin-import -D
  • yarn add antd axios mockjs -S
  • node-sass
  • yarn add node-sass

配置babel文件–antd

// .babelrc
{
  "plugins": [
    ["import", {
      "libraryName": "antd",
      "libraryDirectory": "es",
      "style": "css" // `style: true` 会加载 less 文件
    }]
  ]
}

配置mobx—不需要

  • yarn add mobx mobx-react -s
  • yarn add @babel/plugin-proposal-decorators -D
  • yarn add @babel/plugin-proposal-class-properties -D
"babel": {
    "plugins": [
      [
        "@babel/plugin-proposal-decorators",
        {
          "legacy": true
        }
      ],
      [
        "@babel/plugin-proposal-class-properties",
        {
          "loose": true
        }
      ]
    ],
    "presets": [
      "react-app"
    ]
  }
  "eslintConfig": {
    "parserOptions": {
      "ecmaFeatures": {
        "legacyDecorators": true
      }
    },
    "extends": "react-app"
 },

声明全局的变量

// 举例
import Mockurl from './Mockurl'
declare module 'react'{
  interface Component{
    $mockurl:any
  }
}
Component.prototype
$mockurl = Mockurl

挂在window对象

// store挂在在window对象上
import store from './store'
declare global{
  interface Window{
    store:any
  }
}
window.store = store

antd引用报错

解决措施

  • 删除掉index.tsx中的<React.StrictMode>

StrictMode 目前有助于:

  • 识别不安全的生命周期
  • 关于使用过时字符串 ref API 的警告
  • 关于使用废弃的 findDOMNode 方法的警告
  • 检测意外的副作用
  • 检测过时的 context API
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值