转载:https://www.cnblogs.com/baqiphp/p/7647912.htmlphp
npm initcss
npm install webpack --devhtml
npm install react react-dom @types/react @types/react-dom --savenode
npm install typescript ts-loader source-map-loaderreact
当前根目录下建立tsconfig.json文件webpack
{
"compilerOptions": {
"outDir": "./dist/", //输出目录
"sourceMap": true, //生成对应的sourceMap文件
"noImplicitAny": true, //TypeScript 编译器没法推断出类型时,它仍然会生成 JavaScript 文件
"module": "commonjs", //代码规范,也能够选amd
"target": "es5", //转换成es5
"jsx": "react" //TypeScript具备三种JSX模式:preserve,react和react-native
},
"include": [
"./src/**/*" //须要编译的目录。
]
}
复制代码
import * as React from "react";
export interface ViewProps {
name: string;
age?: number;
}
export interface ViewState {}
export default class Hero extends React.Component<ViewProps,ViewState>{
constructor(props: ViewProps){
super(props);
this.state = {};
}
render(){
const { name, age = 1} = this.props;
return (
<div>
<h6>英雄的信息:</h6>
<p>姓名:{name}</p>
<p>年龄:{age}</p>
</div>
);
}
}
复制代码
import * as React from "react";
import * as ReactDOM from "react-dom";
import Hero from "./components/Hero";
ReactDOM.render(
<Hero name="安其拉" age={5}/>,
document.getElementById("app") as HTMLElement
);
复制代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link rel="shortcut icon" href="./logo.jpg">
<title>Model</title>
</head>
<body>
<noscript>Script syntax is not currently supported</noscript>
<div id="app"></div>
<script src="../dist/bundle.js"></script>
</body>
</html>
复制代码
module.exports = {
entry: "./src/index.tsx",
output: {
filename: "bundle.js",
path: __dirname + "/dist",
publicPath: "./dist/" //打包生成文件访问公共路径
},
devtool: "source-map",
resolve: {
extensions: [".ts", ".tsx", ".js", ".json"]
},
module: {
rules: [
{ test: /\.tsx?$/, loader: "ts-loader" },
{ enforce: "pre", test: /\.js$/, loader: "source-map-loader" }
]
},
plugins: [
],
};
复制代码
webpack --config webpack.common.config.jsweb
npm install webpack-dev-server --devtypescript
const webpack = require('webpack');
//引入公共配置
const config = require('./webpack.common.config');
//配置webpack-dev-server
config.devServer = {
hot: true, //开启热更新
publicPath: '/dist/' //webpack-dev-server编译后资源存放地址
}
config.plugins.push(new webpack.HotModuleReplacementPlugin());
module.exports = config;
复制代码
webpack-dev-server --config webpack.dev.config.jsnpm
打开网页,进入localhots:8080就能够看到咱们的页面了。json
打开浏览器的开发者工具,在console部分能看到如下两句提示就说明热更新启动成功了。
[HMR] Waiting for update signal from WDS... [WDS] Hot Module Replacement enabled.
在package.json的scripts下添加 "start": "webpack-dev-server --config webpack.dev.config.js"
输入npm start 启动服务。
npm install redux react-redux @types/react-redux --save
export interface StoreState {
languageName: string;
enthusiasmLevel?: number;
}
复制代码
export const INCREMENT_ENTHUSIASM = "INCREMENT_ENTHUSIASM";
export type INCREMENT_ENTHUSIASM = typeof INCREMENT_EMTHUSIASM;
export const DECREMENT_ENTHUSIASM = "DECREMENT_ENTHUSIASME";
export type DECREMENT_ENTHUSIAM = typeof DECREMENT_ENTHUSIASM;
复制代码
import * as constants from "../constants";
export interface IncrementEnthusiasm {
type: constants.INCREMENT_ENTHUSIASM;
}
export interface DecrementEnthusiasm {
type: constants.DECREMENT_ENTHUSIASM;
}
export type EnthusiasmAction = IncrementEnthusiasm | DecrementEnthusiasm;
export function incrementEnthusiasm():IncrementEnthusiasm {
return {
type: constants.INCREMENT_ENTHUSIASM
}
}
export function decrementEnthusiasm():DecrementEnthusiasm {
return {
type: constants.DECREMENT_ENTHUSIASM
}
}
复制代码
import { EnthusiasmAction } from "../actions";
import { StoreState } from "../types/index";
import { INCREMENT_ENTHUSIASM, DECREMENT_ENTHUSIASM } from "../constants/index";
export function enthusiasm(state: StoreState,action: EnthusiasmAction):StoreState {
switch (action.type){
case INCREMENT_ENTHUSIASM:
return { ...state, enthusiasmLevel:state.enthusiasmLevel + 1 };
case DECREMENT_ENTHUSIASM:
{ ...state, enthusiasmLevel: Math.max(1,state.enthusiasmLevel - 1)};
default:
return state;
}
}
复制代码
import * as React from "react";
export interface Props {
name: string;
enthusiasmLevel?: number;
onIncrement?: () => void;
onDecrement?: () => void;
}
export default function Hello ({name, enthusiasmLevel = 1, onIncrement, onDecrement}: Props){
if(enthusiasmLevel <= 0) {
throw new Error("error");
}
return (
<div className="hello">
<div className="greeting">
Hello {name + getExclamationMarks(enthusiasmLevel)}
</div>
<div>
<button onClick={onDecrement}>-</button>
<button onClick={onIncrement}>+</button>
</div>
</div>
);
}
function getExclamationMarks(numChars: number) {
return Array(numChars + 1).join("!");
}
复制代码
import Hello from "../components/Hello";
import * as actions from "../actions";
import { StoreState } from "../types/index";
import { connect,Dispatch } from "react-redux";
export function mapStateToProps({enthusiasmLevel,languageName}:StoreState){
return {
enthusiasmLevel,
name: languageName
};
}
export function mapDispatchToProps(dispatch: Dispatch<actions.EnthusiasmAction>){
return {
onIncrement: () => dispatch(actions.incrementEnthusiasm()),
onDecrement: () => dispatch(actions.decrementEnthusiasm())
};
}
export default connect (mapStateToProps,mapDispatchToProps)(Hello);
复制代码
export default {
enthusiasmLevel: 1,
languageName: "TypeScript"
}
复制代码
import { createStore } from "redux";
import { enthusiasm } from "../reducers/index";
import { StoreState } from "./initState";
export default function() {
const store = createStore<StoreState>(enthusiasm,initState);
export store;
}
复制代码
import * as React from "react";
import * as ReactDOM from "react-dom";
import Hello from "./containers/Hello";
import { Provider } from "react-redux";
import configureStore from "./store/configureStore";
const store = configureStore();
ReactDOM.render(
<Provider store={store}>
<Hello />
</Provider>,
document.getElementById("root") as HTMLElement
);
复制代码
》》》待完善
npm install css-loader style-loader --dev
{ test: /.css$/, loader: "style-loader!css-loader?modules" }
{ test: /.css$/,loader: "style-loader!css-loader", include: /node_modules/ }
{ test: /.css$/,loader: "style-loader!css-loader?modules", exclude: /node_modules/ }
npm install file-loader --dev
{ test: /.(png|jpe?g|gif)/, loader: "file-loader" }