npx create-react-app test # test 爲你須要建立項目的名字,會在命令當前目錄下建立test的目錄,包含項目全部的文件
你已經完成了建立,開始跑起來css
npm start
你能夠看到react已經可以在local host:3000訪問了,只有一個歡迎頁面html
是當前目錄安裝的模塊存放的地方前端
index.html 是單頁面的入口node
可存放本身編寫的代碼,App是默認生成的歡迎頁面邏輯,index 是js的主入口react
1.將App.js的代碼更改以下git
import React, {Component} from 'react'; import './App.css'; class App extends Component { constructor(props) { super(props) this.state = {todos: [{checked: false, text: "hello"}, {checked: true, text: "world"}]} this.handleClick=this.handleClick.bind(this) } handleClick(index) { let todos = this.state.todos todos[index].checked = !todos[index].checked this.setState({todos:todos}) } render() { let todos = this.state.todos let todosDiv = todos.map((item, index) => { return (<Todo index={index} checked={item.checked} text={item.text} handleClick={this.handleClick}/>) }) return ( <div className="App"> {todosDiv} </div> ); } } class Todo extends Component { constructor(props){ super(props) this.handleClick=this.handleClick.bind(this) } handleClick() { let index = this.props.index this.props.handleClick(index) }; render() { return ( <p><input type={'checkbox'} checked={this.props.checked} onClick={this.handleClick}/> {this.props.text}:{this.props.index} </p> ) } } export default App;
四五層組件的時候得一步一步的往上級傳遞,這就會致使組件傳遞寫的很臃腫。這個時候就須要一個將狀態(即state這個值)獨立開來。github
安裝依賴spring
npm install mobx --save npm install mobx-react --save
啓用裝飾器語法npm
# 若是有git的話,要將沒有保存的文件上傳以後或者刪除以後才能跑eject命令 yarn run eject npm install --save-dev babel-preset-mobx
在package.json中找到babel項目,在presets裏面增長"mobx"json
"babel": { "presets": [ "react-app", "mobx" ]},
加入core-decorators
npm install core-decorators --save
在src下增長store.AppStore.js文件
import {action, observable} from "mobx"; class AppStore { @observable todos; constructor() { this.todos = [{checked: false, text: "hello"}, {checked: true, text: "world"}] } @action.bound handleClick(index) { let todos = this.todos todos[index].checked = !todos[index].checked } } export default AppStore;
改寫index.js
import React from 'react'; import ReactDOM from 'react-dom'; import './index.css'; import App from './App'; import * as serviceWorker from './serviceWorker'; import {Provider} from "mobx-react"; import AppStore from './store/AppStore' let rootStore = {} rootStore['app'] = new AppStore() ReactDOM.render( <Provider {...rootStore}> <App/> </Provider>, document.getElementById('root')); // If you want your app to work offline and load faster, you can change // unregister() to register() below. Note this comes with some pitfalls. // Learn more about service workers: https://bit.ly/CRA-PWA serviceWorker.unregister();
改寫App.js
import React, {Component} from 'react'; import './App.css'; import {inject, observer} from "mobx-react"; import {autobind} from "core-decorators"; @inject("app") @autobind @observer class App extends Component { constructor(props) { super(props) } render() { let todos = this.props.app.todos let todosDiv = todos.map((item, index) => { return (<Todo index={index}/>) }) return ( <div className="App"> {todosDiv} </div> ); } }
@inject("app") @autobind @observer class Todo extends Component { constructor(props) { super(props) } handleClick() { let index = this.props.index this.props.app.handleClick(index) }; render() { let index = this.props.index let todo = this.props.app.todos[index] return ( <p><input type={'checkbox'} checked={todo.checked} onClick={this.handleClick}/> {todo.text}:{index} </p> ) } } export default App; ```
安裝依賴
npm install react-router mobx-react-router --save
增長新的頁面,在src中增長component/Test.js
import * as React from "react"; class Test extends React.Component{ render() { return(<p>welcome!</p>) } } export default Test;
更改index.js
import React from 'react'; import ReactDOM from 'react-dom'; import './index.css'; import App from './App'; import * as serviceWorker from './serviceWorker'; import {Provider} from "mobx-react"; import AppStore from './store/AppStore' import {Route, Router, Switch} from "react-router"; import {RouterStore, syncHistoryWithStore} from "mobx-react-router"; import createHashHistory from "history/createHashHistory" import Test from "./component/Test" let rootStore = {} const hashHistory = createHashHistory() const routerStore = new RouterStore() const history = syncHistoryWithStore(hashHistory, routerStore) rootStore['app'] = new AppStore() routerStore['routing'] = routerStore ReactDOM.render( <Provider {...rootStore}> <Router history={history}> <p>here is the menu</p> <Switch> <Route path={"/test"} component={Test}/> <Route path={"/"} component={App}/> </Switch> </Router> </Provider>, document.getElementById('root')); // If you want your app to work offline and load faster, you can change // unregister() to register() below. Note this comes with some pitfalls. // Learn more about service workers: https://bit.ly/CRA-PWA serviceWorker.unregister();
按照以前建立的步驟,建立前端的模塊,假設模塊名字爲view,並在前端模塊的目錄下增長pom.xml
<build> <plugins> <plugin> <groupId>com.github.eirslett</groupId> <artifactId>frontend-maven-plugin</artifactId> <version>1.2</version> <executions> <-- Install our node and npm version to run npm/node scripts--> <execution> <id>install node and npm</id> <goals> <goal>install-node-and-npm</goal> </goals> <configuration> <-- 指定node的版本例如 v6.9.1 --> <nodeVersion>${nodeVersion}</nodeVersion> <npmVersion>${npmVersion}</npmVersion> <nodeDownloadRoot>https://npm.taobao.org/mirrors/node/</nodeDownloadRoot> <npmDownloadRoot>http://registry.npmjs.org/npm/-/</npmDownloadRoot> </configuration> </execution> <-- Set NPM Registry --> <execution> <id>npm set registry</id> <goals> <goal>npm</goal> </goals> <configuration> <--<arguments>config set registry https://registry.npmjs.org</arguments>--> <arguments>config set registry https://registry.npm.taobao.org</arguments> </configuration> </execution> <-- Set SSL privilege --> <execution> <id>npm set non-strict ssl</id> <goals> <goal>npm</goal> </goals> <-- Optional configuration which provides for running any npm command --> <configuration> <arguments>config set strict-ssl false</arguments> </configuration> </execution> <-- Install all project dependencies --> <execution> <id>npm install</id> <goals> <goal>npm</goal> </goals> <-- optional: default phase is "generate-resources" --> <phase>generate-resources</phase> <-- Optional configuration which provides for running any npm command --> <configuration> <arguments>install</arguments> </configuration> </execution> <-- Build and minify static files --> <execution> <id>npm run build</id> <goals> <goal>npm</goal> </goals> <configuration> <arguments>run build</arguments> </configuration> </execution> </executions> </plugin> </plugins> </build>
在spring boot後端項目中,將前端打包好的頁面拷貝到後端目錄中
<build> <plugins> <plugin> <artifactId>maven-resources-plugin</artifactId> <executions> <execution> <id>Copy App Content</id> <phase>generate-resources</phase> <goals> <goal>copy-resources</goal> </goals> <configuration> <outputDirectory>src/main/resources/public</outputDirectory> <overwrite>true</overwrite> <resources> <resource> <directory>${project.parent.basedir}/view/build</directory> <includes> <include>static/</include> <include>index.html</include> </includes> </resource> </resources> </configuration> </execution> </executions> </plugin> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>
"proxy":"http://localhost:8080/"
方法1: package.json 增長
"homepage": "."
方法2: config.paths.js文件下修改配置
function getServedPath(appPackageJson) { const publicUrl = getPublicUrl(appPackageJson); //將/修改成./ const servedUrl = envPublicUrl || (publicUrl ? url.parse(publicUrl).pathname : './'); return ensureSlash(servedUrl, true); }
<plugin> <artifactId>maven-archetype-plugin</artifactId> <version>3.0.1</version> <configuration> <propertyFile>archetype.properties</propertyFile> </configuration> </plugin>
const routerStore = new RouterStore() rootStore['app'] = new AppStore() routerStore['routing'] = routerStore
rootStore['app'] = new AppStore(rootStore)
改寫AppStore.js,增長構造函數
constructor(rootStore) { this.rootStore = rootStore }