dva与create-react-app的结合使用
dva与我们的create-react-app创建的两款脚手架是我们写react项目的两款优秀框架,之前一种使用create-react-app这款脚手架进行开发。然后这个框架美中不足的是redux方面着实令人书写麻烦 然而dva框架就对其进行了封装改良 使其看起来vuex相似度十分高 下面给大家介绍下用这两个框架结合起来开发的步骤吧
首先利用create-react-app生成一个脚手架 然后下载一个dva和history包
接着把脚手架生成的src全部删掉 用我们自己的项目目录进行开发
首先 建立一个index.js文件夹 作为我们项目的主入口
import dva from "dva";
import App from "./App" //注意这种引入方式已经报销 在控制台会报警告
// import createHistory from 'history/createBrowserHistory'; //这种为history路由展示方式
//const createHistory=require("history").createBrowserHistory //这种为hash路由展示方式
const createHistory=require("history").createHashHistory //下面是两种注入方式
const app = dva({
history: createHistory(),
}); //const app=dva(createHistory); app.router(App) app.start('#root');
然后建立一个App.jsx作为我们项目路由的主出口文件
import React,{Suspense} from 'react' import {Router} from "dva/router" import RouteView from "./routes/RouteView"
import RouteConfig from "./routes/RouteConfig" //注意这里一定要注入history不然页面展示不出来 而且是结构出来的history
function App({history}){
return <Router history={history}>
<Suspense fallback={<div>loading...</div>}>
<RouteView children={RouteConfig}></RouteView>
</Suspense> </Router>
} export default App
为什么使用这个App.jsx呢?
---原因当然是想使用路由表了 为了让其与vue项目相似度更高些哈哈 下面展示下个人封装的两个路由表
RouteView
import React, { Component } from 'react' import {Switch,Redirect,Route} from "dva/router" export default class RouteView extends Component {
render() {
return (
<Switch>
{this.props.children.map((item,index)=>{
if(item.redirect){
return <Redirect key={index} from={item.path} to={item.redirect}></Redirect>
}else{
return <Route key={index} path={item.path} render={(props)=>{
return <item.component children={item.children} {...props}></item.component>
}}></Route>
}
})}
</Switch>
)
}
}
RouteConfig
/*
* @Author: chenqiang
* @Date: 2019-08-16 10:59:42
* @Last Modified by: chenqiang
* @Last Modified time: 2019-08-17 20:19:41
* 路由配置表
*/
import React from "react";
const RouteConfig=[
{
path:"/main",
component:React.lazy(()=>import("@/views/main/index.jsx")),
children:[
{
path:"/main/home",
component:React.lazy(()=>import("@/views/main/home/index.jsx"))
},{
path:"/main",
redirect: "/main/home"
}
]
}, {
path:"/login",
component:React.lazy(()=>import("@/views/login/index.jsx"))
},{
path:"/register",
component:React.lazy(()=>import("@/views/register/index.jsx"))
},{
path:"/",
redirect:"/main"
}
] export default RouteConfig;
然后此时我们就可以放心大胆的yarn start运行我们的项目了
很令人无语的是控制台爆出了这么一个警告 虽然不影响我们的代码执行,但是有强迫症的人来说看见这篇警告总是非常不爽的一件事
于是就各种查阅资料进行取消这个警告
先说下这个警告的意思吧 说第一种引入的方式已被废除让我们用下面哪种引入方式进行引入 然后是不是很神奇呢 ,我明明没有这样
引入过为什么给我报出来了呢??? 答案在下面===>
打开我们的node_modulex 找到我们dva包 打开下面这个文件
然后在里面就会有哪种方式的引入 将其更改掉就好了
美中不足的是每次重新下包就会给你自动改回去~~~
剩下的就可以放心大胆的撸代码了 然后下面在贴一个antd的按需加载
建造一个.babelrc文件 内容如下
{
"presets": [
"react-app"
],
"plugins": [
[
"import",
{
"libraryName": "antd",
"libraryDirectory": "es",
"style": "css" // `style: true` 会加载 less 文件
}
]
]
}
注意package.json 不能再有babelrc这个字段了 不然会报错
dva与create-react-app的结合使用的更多相关文章
- tap news:week5 0.0 create react app
参考https://blog.csdn.net/qtfying/article/details/78665664 先创建文件夹 安装create react app 这个脚手架(facebook官方提 ...
- 使用create react app教程
This project was bootstrapped with Create React App. Below you will find some information on how to ...
- 如何扩展 Create React App 的 Webpack 配置
如何扩展 Create React App 的 Webpack 配置 原文地址https://zhaozhiming.github.io/blog/2018/01/08/create-react-a ...
- 深入 Create React App 核心概念
本文差点难产而死.因为总结的过程中,多次怀疑本文是对官方文档的直接翻译和简单诺列:同时官方文档很全面,全范围的介绍无疑加深了写作的心智负担.但在最终的梳理中,发现走出了一条与众不同的路,于是坚持分享出 ...
- 在 .NET Core 5 中集成 Create React app
翻译自 Camilo Reyes 2021年2月22日的文章 <Integrate Create React app with .NET Core 5> [1] Camilo Reyes ...
- Create React App
Facebook开源了React前端框架(MIT Licence),也同时提供了React脚手架 - create-react-app. create-react-app遵循约定优于配置(Coc)的原 ...
- Create React App 安装less 报错
执行npm run eject 暴露模块 安装 npm i less less-loader -D 1.打开 react app 的 webpack.config.js const sassRege ...
- [React] Use the Fragment Short Syntax in Create React App 2.0
create-react-app version 2.0 added a lot of new features. One of the new features is upgrading to Ba ...
- [React] {svg, css module, sass} support in Create React App 2.0
create-react-app version 2.0 added a lot of new features. One of the new features is added the svgr ...
- create react app 项目部署在Spring(Tomcat)项目中
网上看了许多,大多数都是nginx做成静态项目,但是这样局限性太多,与Web项目相比许多服务端想做的验证都很麻烦,于是开始了艰难的探索之路,终于在不经意间试出来了,一把辛酸... 正常的打包就不说了. ...
随机推荐
- 跟我一起做一个vue的小项目(八)
接下来我们进行的是城市选择页面的路由配置 添加city.vue,使其点击城市,然后跳转到city页面 //router.js import Vue from 'vue' import Router f ...
- 关于 webpack的总结
一 . 几个基本的概念 1. mode开发模式 // webpack.production.config.js module.exports = { mode: 'production' // 生产模 ...
- Spring MVC 搭建web项目示例
环境为Eclipse 1:新建Dynamic web project : springMvcDemo 2:下载spring的jar包,把jar包复制到WEB-INF/lib目录下 3.添加配置文件w ...
- jnhs-Myeclipse 10注册教程unable to access jarfile cracker.jar
直接双击jar文件就可以 打开后,随便写一个名字 然后复制LICENSE_KEY的内容,打开myeclipse 在Code那里粘贴你刚才复制的内容,然后点击Save & Active Now ...
- oracle基本认识
概要图 1. 环境搭建 1.1 Oracle的安装 数据库的三个常用的用户及默认密码sys:change_on_installsystem:managerscott:tiger Oracle客户端: ...
- BZOJ 3296: [USACO2011 Open] Learning Languages
Time Limit: 5 Sec Memory Limit: 128 MB Submit: 387 Solved: 206 [Submit][Status][Discuss] Description ...
- zoj 1028 Flip and Shift(数学)
Flip and Shift Time Limit: 2 Seconds Memory Limit: 65536 KB This puzzle consists of a random se ...
- 常用css3属性
总结一下在工作用常用到的属性设置 1.设置文本的可选择性 -webkit-user-select:none/text 2.设置背景的绘制区域 background-clip:border-box/pa ...
- 继续对dubbo源代码进行maven builder
版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/enson16855/article/details/32073981 原文地址:http://dtb ...
- vue中router以及route的使用
路由基本概念 route,它是一条路由. { path: '/home', component: Home } routes,是一组路由. const routes = [ { path: '/hom ...