本文包括如下几个步骤:

  • 新建Demo项目
  • 新建Demo模块
  • 编写config.js
  • 编写package.json
  • 编写demo入口文件
  • 页面访问

新建Demo项目

本地新建一个空的项目choerodon-todo-service

  1. $ mkdir -p react/src/app/demo/containers

编写config.js

在react文件夹下创建config.js

  1. $ cd react
  2. $ touch config.js

在项目根目录下创建package.json

  1. $ cd ..
  2. $ npm init
  1. // package.json
  2. {
  3. "name": "@choerodon/demo", // name为模块名(可以增加@choerodon scope)
  4. "routeName": "demo", // routeName为路由前缀(如空,取name为路由前缀)
  5. "version": "1.0.0",
  6. "description": "",
  7. "main": "./react/src/app/demo/containers/DEMOIndex.js", // main为入口index的路径(如空,当前模块不会被编译进去,一般只有总前端类型不设置)
  8. "scripts": {
  9. "start": "choerodon-front-boot start --config ./react/config.js",
  10. "lint-staged": "lint-staged",
  11. "lint-staged:es": "eslint",
  12. "compile": "gulp compile"
  13. },
  14. "contributors": [
  15. "choerodon"
  16. ],
  17. "dependencies": {
  18. "@choerodon/boot": "0.17.x",
  19. "@choerodon/master": "0.17.x" // 表示进入页面后的部分,菜单、header和AutoRouter等,可自己配置
  20. },
  21. "files": [
  22. "lib"
  23. ],
  24. "lint-staged": {
  25. "react/**/*.{js,jsx}": [
  26. "npm run lint-staged:es"
  27. ],
  28. "react/**/*.scss": "stylelint --syntax scss"
  29. },
  30. "husky": {
  31. "hooks": {
  32. "pre-commit": "lint-staged"
  33. }
  34. },
  35. "babel-preset-env": "^1.7.0",
  36. "gulp": "^3.9.1",
  37. "gulp-babel": "^7.0.1",
  38. "through2": "^2.0.3"
  39. }
  40. }

编写demo入口文件

containers文件夹用于存放前端的页面。

  1. // DEMOIndex.js
  2. import React from 'react';
  3. import { Route, Switch } from 'react-router-dom';
  4. import { inject } from 'mobx-react';
  5. import { asyncRouter, nomatch } from '@choerodon/boot';
  6. @inject('AppState')
  7. class DEMOIndex extends React.Component {
  8. render() {
  9. const { match, AppState } = this.props;
  10. return (
  11. <Switch>
  12. <Route path="*" component={nomatch} />
  13. </Switch>
  14. );
  15. }
  16. }
  17. export default DEMOIndex;

package.json 同级目录下,安装并启动。

  1. $ npm install