dva 打包多个html,使用dva+umi+antd构建页面(一)

使用dva+umi+antd构建页面

首先确保安装npm或者yarnhtml

建立新应用

首先建立应用目录node

mkdir myapp && cd myapp

推荐使用 yarn 建立应用,执行如下命令,react

若是你坚持用 npm,可执行 npm install -g create-umi && create-umi,效果一致。webpack

$ yarn create umi

yarn create v1.12.0

[1/4] ? Resolving packages...

[2/4] ? Fetching packages...

[3/4] ? Linking dependencies...

[4/4] ? Building fresh packages...

success Installed "create-umi@0.9.5" with binaries:

- create-umi

yarn 会先安装最新版的 create-umi,而后提供交互式的提示来建立应用。git

选择 app, 而后回车确认。github

? Select the boilerplate type

ant-design-pro - Create project with an layout-only ant-design-pro boilerplate, use together with umi block.

❯ app - Create project with a simple boilerplate, support typescript.

block - Create a umi block.

library - Create a library with umi.

plugin - Create a umi plugin.

选上 antd 和 dva,而后回车确认。web

create package.json

create mock/.gitkeep

create src/assets/yay.jpg

create src/layouts/index.css

create src/layouts/index.js

create src/pages/index.css

create src/pages/index.js

create src/global.css

create .gitignore

create .editorconfig

create .env

create .umirc.js

create .eslintrc

create .prettierrc

create .prettierignore

create src/models/.gitkeep

create src/dva.js

✨ File Generate Done

✨ Done in 966.73s.

而后安装依赖,typescript

$ yarn

而后启动应用,shell

$ yarn start

几秒钟后,你会看到如下输出,

DONE Compiled successfully in 212ms

App running at:

- Local: http://localhost:8000/

- Network: http://{{ YourIP }}:8000/

在浏览器里打开 http://localhost:8000,你会看到 umi 的欢迎界面。

949e09b4ce21104f7beb1a090b1fe73d.png

使用 antd#

前面选择 antd 以后,会自动处理 antd 的依赖以及按需加载。你能够检查 .umirc.js 里的配置,确保 antd 已开启。

// ref: https://umijs.org/config/

export default {

plugins: [

// ref: https://umijs.org/plugin/umi-plugin-react.html

['umi-plugin-react', {

antd: true,

dva: true,

}],

],

}

而若是要使用固定版本的 antd,你能够在项目里安装额外的 antd 依赖,package.json 里声明的 antd 依赖会被优先使用。

新建路由#

咱们要写个应用来先显示产品列表。首先第一步是建立路由,路由能够想象成是组成应用的不一样页面。

若是你没有 npx,须要先安装他,用于执行 node_modules 下的命令,

$ yarn global add npx

而后经过命令建立 /products 路由,

$ npx umi g page products

create src/pages/products.js

create src/pages/products.css

✔ success

编写 UI Component#

随着应用的发展,你会须要在多个页面分享 UI 元素 (或在一个页面使用屡次),在 umi 里你能够把这部分抽成 component 。

咱们来编写一个 ProductList component,这样就能在不一样的地方显示产品列表了。

新建 src/components/ProductList.js 文件:

import { Table, Popconfirm, Button } from 'antd';

const ProductList = ({ onDelete, products }) => {

const columns = [{

title: 'Name',

dataIndex: 'name',

}, {

title: 'Actions',

render: (text, record) => {

return (

onDelete(record.id)}>

Delete

);

},

}];

return (

dataSource={products}

columns={columns}

/>

);

};

export default ProductList;

定义 dva Model#

完成 UI 后,如今开始处理数据和逻辑。

dva 经过 model 的概念把一个领域的模型管理起来,包含同步更新 state 的 reducers,处理异步逻辑的 effects,订阅数据源的 subscriptions 。

新建 model src/models/products.js,

export default {

namespace: 'products',

state: [],

reducers: {

'delete'(state, { payload: id }) {

return state.filter(item => item.id !== id);

},

},

};

这个 model 里:

namespace 表示在全局 state 上的 key

state 是初始值,在这里是空数组

reducers 等同于 redux 里的 reducer,接收 action,同步更新 state

umi 里约定 src/models 下的 model 会被自动注入,你无需手动注入。

connect 起来#

到这里,咱们已经单独完成了 model 和 component,那么他们如何串联起来呢?

dva 提供了 connect 方法。若是你熟悉 redux,这个 connect 来自 react-redux。

编辑 src/pages/products.js,替换为如下内容:

import { connect } from 'dva';

import ProductList from '../components/ProductList';

const Products = ({ dispatch, products }) => {

function handleDelete(id) {

dispatch({

type: 'products/delete',

payload: id,

});

}

return (

List of Products

);

};

export default connect(({ products }) => ({

products,

}))(Products);

最后,咱们还须要一些初始数据让这个应用 run 起来。编辑 src/app.js:

export const dva = {

config: {

onError(err) {

err.preventDefault();

console.error(err.message);

},

initialState: {

products: [

{ name: 'dva', id: 1 },

{ name: 'antd', id: 2 },

],

},

},

};

刷新浏览器,应该能看到如下效果:

623ed7915df22657193fdd17e1414d9c.png

构建应用#

完成开发而且在开发环境验证以后,就须要部署给咱们的用户了。先执行下面的命令,

$ npm run build

几秒后,输出应该以下:

> @ build /private/tmp/sorrycc-V0lLrF

> umi build

[5:01:58 PM] webpack compiled in 11s 615ms

DONE Compiled successfully in 11622ms 5:01:58 PM

File sizes after gzip:

340.44 KB dist/umi.js

17.82 KB dist/umi.css

build 命令会打包全部的资源,包含 JavaScript, CSS, web fonts, images, html 等。你能够在 dist/ 目录下找到这些文件。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值