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
Copy after login
-
. 使用 dva-cli 创建项目, 创建好的项目目录如下:
dva new 01-dva-quickstart
Copy after login
-
. 安装 typescript 所需的 react, react-dom 包, 以及 ts-loader 和 tslint
npm install --save-dev @types/react @types/react-dom ts-loader ts-lint
Copy after login -
. 配置 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" ] }
Copy after login -
. 配置 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!

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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Yes, MySQL can be installed on Windows 7, and although Microsoft has stopped supporting Windows 7, MySQL is still compatible with it. However, the following points should be noted during the installation process: Download the MySQL installer for Windows. Select the appropriate version of MySQL (community or enterprise). Select the appropriate installation directory and character set during the installation process. Set the root user password and keep it properly. Connect to the database for testing. Note the compatibility and security issues on Windows 7, and it is recommended to upgrade to a supported operating system.

Pull vertical guides in Photoshop: Enable ruler view (View > ruler). Hover the mouse over the vertical edge of the ruler, and then the cursor becomes a vertical line with double arrows and hold and drag the mouse to pull out the reference line. Click Delete by dragging the guide, or hovering it into a cross.

Unable to access MySQL from the terminal may be due to: MySQL service not running; connection command error; insufficient permissions; firewall blocks connection; MySQL configuration file error.

The MySQL connection may be due to the following reasons: MySQL service is not started, the firewall intercepts the connection, the port number is incorrect, the user name or password is incorrect, the listening address in my.cnf is improperly configured, etc. The troubleshooting steps include: 1. Check whether the MySQL service is running; 2. Adjust the firewall settings to allow MySQL to listen to port 3306; 3. Confirm that the port number is consistent with the actual port number; 4. Check whether the user name and password are correct; 5. Make sure the bind-address settings in my.cnf are correct.

The solution to MySQL installation error is: 1. Carefully check the system environment to ensure that the MySQL dependency library requirements are met. Different operating systems and version requirements are different; 2. Carefully read the error message and take corresponding measures according to prompts (such as missing library files or insufficient permissions), such as installing dependencies or using sudo commands; 3. If necessary, try to install the source code and carefully check the compilation log, but this requires a certain amount of Linux knowledge and experience. The key to ultimately solving the problem is to carefully check the system environment and error information, and refer to the official documents.

The main reasons for MySQL installation failure are: 1. Permission issues, you need to run as an administrator or use the sudo command; 2. Dependencies are missing, and you need to install relevant development packages; 3. Port conflicts, you need to close the program that occupies port 3306 or modify the configuration file; 4. The installation package is corrupt, you need to download and verify the integrity; 5. The environment variable is incorrectly configured, and the environment variables must be correctly configured according to the operating system. Solve these problems and carefully check each step to successfully install MySQL.

export default export in Vue is a value that can be an object, function, or any JavaScript value, depending on what follows the export default. Therefore, it exports: Component Options Object (used to create component instances) Function Normal JavaScript Object

MySQL download prompts a disk write error. The solution is as follows: 1. Check whether the disk space is insufficient, clean up the space or replace a larger disk; 2. Use disk detection tools (such as chkdsk or fsck) to check and fix disk errors, and replace the hard disk if necessary; 3. Check the target directory permissions to ensure that the user account has write permissions; 4. Change the download tool or network environment, and use the download manager to restore interrupted download; 5. Temporarily close the anti-virus software or firewall, and re-enable it after the download is completed. By systematically troubleshooting these aspects, the problem can be solved.
