vscode builds a development environment for Typescript+React+Dva
这篇文章主要介绍了关于vscode搭建Typescript+React+Dva的开发环境,有着一定的参考价值,现在分享给大家,有需要的朋友可以参考一下
[ 作为2018年前端最应该学的技术 ], Typescript 确实惹火, 这两天崩崩也是自学了一下 ts. 并且配置了一个简单的基于 Dva+React+Typescript+Tslint 的环境, 由于其他的百度教程都是 17 年或者更早, 很多已经过时了, 所以想想还是分享经验给自学的friends!
第一步
安装 typescript (推荐使用全局安装)
npm install -g typescript
第二步
安装 dva-cli(使用全局安装)
npm install -g dva-cli
第三步
-
. 进入你自己的项目目录
cd d:/code/4-Dva+React\1-dva+react_firstday
-
. 使用 dva-cli 创建项目, 创建好的项目目录如下:
dva new 01-dva-quickstart
-
. 安装 typescript 所需的 react, react-dom 包, 以及 ts-loader 和 tslint
npm install --save-dev @types/react @types/react-dom ts-loader ts-lint
-
. 配置 tsconfig.json ( ts配置项 )
在项目 根目录 下新建 tsconfig.json(
./tsconfig.json
), 并写入下列必须代码:{ "compilerOptions": { "strictNullChecks": true, "moduleResolution": "node", "allowSyntheticDefaultImports": true, "experimentalDecorators": true, "jsx": "preserve", "noUnusedParameters": true, "noUnusedLocals": true, "target": "es6", "lib": [ "dom", "es7" ] }, "exclude": [ "node_modules", "lib", "es" ] }
-
. 配置 tslint.json ( tslint规范 )
在项目 根目录 下新建 tslint.json(
./tslint.json
), 写入下列必须代码:
(ps:下面的 rules 配置项, 可以自行添加)
{ "extends": [ "tslint:latest", "tslint-react" ], "linterOptions": { "exclude": [ "node_modules/**/*.ts", "src/**/*.{ts,tsx}" ] }, "rules": { "object-literal-sort-keys": false, "jsx-no-lambda": false, "eofline": false, "no-consecutive-blank-lines": false, "no-var-requires": false, "quotemark": false, "space-within-parents": false, "no-submodule-imports": false, "no-implicit-dependencies": false, "ordered-imports": [ false ], "jsx-boolean-value": false, "no-trailing-whitespace": false, "semicolon": false, "trailing-comma": false, "space-in-parents": false, "typedef-whitespace": [ false ], "whitespace": [ true ], "interface-over-type-literal": true, "no-duplicate-imports": false, "no-namespace": true, "no-internal-module": true } }
. 至此, ts 的相关配置已经全部完成, 接着该测试一下啦?
第四步
1 . 删除 ./src 目录下的所有文件, 不要删文件夹;
2 . 在 ./src/routes 目录下新建 Home.tsx(./src/routes/Home.tsx
)(默认首页);Ps: dva 中 routes 相当于 redux 的 containers(容器组件), 具体相关概念可以参考链接描述 , Home.tsx 的代码如下:
import * as React from 'react'; export interface IAppProps { name?: string; }; const Home: React.SFC<iappprops> = (props: IAppProps): JSX.Element => { return ( <p> </p> <h1> Hello {props.name ? props.name : "崩崩呢"} </h1> ); }; export default Home;</iappprops>
3 . 在 ./src/routes 目录下新建 News.tsx ( ./src/routes/News.tsx
)(这是二级页面);
import * as React from 'react'; export interface INewsProps { newslist?: Array; }; const News: React.SFC<inewsprops> = ( props: INewsProps ): JSX.Element => { const newslist = props.newslist ? props.newslist : [ { title: 'title1', content: 'content1', } ]; return ( <p> <nav> <ol> <li>{newslist[0].title}</li> <li>{newslist[0].content}</li> <li>over</li> </ol> </nav> </p> ); };</inewsprops>
4 . 写好了我们的容器组件, 进入到 ./src, 在项目根目录下新建 router.tsx( ./src/router.tsx
), 配置我们的路由!
import * as React from 'react'; import { Router, Route, Switch } from 'dva/router'; import Home from './routes/Home'; // 引入 首页 组件 import News from './routes/News'; // 引入 二级 页面 export default function RouterConfig ({ history }){ // 路由配置 return ( <router> <switch> <route></route> <route></route> </switch> </router> ); }
5 . 最后一步, 去到 ./src/ 根目录, 新建 index.tsx(./src/index.tsx
), 并写入如下代码!
import dva from 'dva'; import createhistory from 'history/createBrowserHistory'; const app = dva({ history: createhistory(), }); app.router( require('./router').default ); app.start('#root');
Ps: 由于 dva 的默认路由是 Hash 路由, 崩崩有点强迫, 所以在 const app = dva({})
中使用了 H5 的 historyAPI, 比较好看;
6 . 在命令行执行npm start
, 代码没写错的话,应该就能看到这样的主页![]()
7 . 继续在浏览器地址栏中输入 /news, 即可看到跳转到了 news 页面![]()
第五步 (大功告成)
总结: 崩崩只学了 2 天的 ts,所以就迫不及待的将其融入到了 redux+react
的实践中, 期间踩了不少的坑, 发现 redux 其实对 ts 的支持不是很友好, 所以果断地转向了更加轻
量的 dva, 而网上的对 dva+react+ts 的配置少之又少,而且是过时的东西, 所以分享给大家..., 另外代码没有过多的注释, dva 文档链接描述 已经讲得很详细了, 崩崩觉得没必要再说了!It’s time to go to bed, there’s only so much code, I will always love the front-end crash!!
The above is the entire content of this article, I hope it will be helpful to everyone’s study, more related content Please pay attention to PHP Chinese website!
Related recommendations:
vue-cli 2.x project optimization introduces local static library files
The above is the detailed content of vscode builds a development environment for Typescript+React+Dva. For more information, please follow other related articles on the PHP Chinese website!

Python and JavaScript have their own advantages and disadvantages in terms of community, libraries and resources. 1) The Python community is friendly and suitable for beginners, but the front-end development resources are not as rich as JavaScript. 2) Python is powerful in data science and machine learning libraries, while JavaScript is better in front-end development libraries and frameworks. 3) Both have rich learning resources, but Python is suitable for starting with official documents, while JavaScript is better with MDNWebDocs. The choice should be based on project needs and personal interests.

The shift from C/C to JavaScript requires adapting to dynamic typing, garbage collection and asynchronous programming. 1) C/C is a statically typed language that requires manual memory management, while JavaScript is dynamically typed and garbage collection is automatically processed. 2) C/C needs to be compiled into machine code, while JavaScript is an interpreted language. 3) JavaScript introduces concepts such as closures, prototype chains and Promise, which enhances flexibility and asynchronous programming capabilities.

Different JavaScript engines have different effects when parsing and executing JavaScript code, because the implementation principles and optimization strategies of each engine differ. 1. Lexical analysis: convert source code into lexical unit. 2. Grammar analysis: Generate an abstract syntax tree. 3. Optimization and compilation: Generate machine code through the JIT compiler. 4. Execute: Run the machine code. V8 engine optimizes through instant compilation and hidden class, SpiderMonkey uses a type inference system, resulting in different performance performance on the same code.

JavaScript's applications in the real world include server-side programming, mobile application development and Internet of Things control: 1. Server-side programming is realized through Node.js, suitable for high concurrent request processing. 2. Mobile application development is carried out through ReactNative and supports cross-platform deployment. 3. Used for IoT device control through Johnny-Five library, suitable for hardware interaction.

I built a functional multi-tenant SaaS application (an EdTech app) with your everyday tech tool and you can do the same. First, what’s a multi-tenant SaaS application? Multi-tenant SaaS applications let you serve multiple customers from a sing

This article demonstrates frontend integration with a backend secured by Permit, building a functional EdTech SaaS application using Next.js. The frontend fetches user permissions to control UI visibility and ensures API requests adhere to role-base

JavaScript is the core language of modern web development and is widely used for its diversity and flexibility. 1) Front-end development: build dynamic web pages and single-page applications through DOM operations and modern frameworks (such as React, Vue.js, Angular). 2) Server-side development: Node.js uses a non-blocking I/O model to handle high concurrency and real-time applications. 3) Mobile and desktop application development: cross-platform development is realized through ReactNative and Electron to improve development efficiency.

The latest trends in JavaScript include the rise of TypeScript, the popularity of modern frameworks and libraries, and the application of WebAssembly. Future prospects cover more powerful type systems, the development of server-side JavaScript, the expansion of artificial intelligence and machine learning, and the potential of IoT and edge computing.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Chinese version
Chinese version, very easy to use

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version
Useful JavaScript development tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment