轉載: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" }