React + MobX 入门及实例(二)
发布时间:2020-12-15 06:38:14 所属栏目:百科 来源:网络整理
导读:在上一章 React + MobX 入门及实例(一) 应用实例TodoList的基础上 增加ant-design优化界面 增加后台express框架,mongoose操作。 增加mobx异步操作fetch后台数据。 步骤 Ⅰ. ant-design 安装antd包 npm install antd --save 安装antd按需加载依赖 npm inst
在上一章 React + MobX 入门及实例(一) 应用实例TodoList的基础上
步骤Ⅰ. ant-design
{ "presets": ["react-native-stage-0/decorator-support"],"plugins": [ [ "import",{ "libraryName": "antd","style": true } ] ],"sourceMaps": true }
import { Button } from 'antd'; Ⅱ. express,mongodb前提:mongodb的安装与配置
引入body-parser中间件,作用是对post请求的请求体进行解析,转换为我们需要的格式。 //express const express = require('express'); const app = express(); //中间件 const bodyParser = require('body-parser'); app.use(bodyParser.urlencoded({extended: false}));// for parsing application/json app.use(bodyParser.json()); // for parsing application/x-www-form-urlencoded //mongoose const mongoose = require('mongoose'); mongoose.connect('mongodb://localhost/todolist',{useMongoClient:true}); mongoose.Promise = global.Promise; const db = mongoose.connection; db.on('error',console.error.bind(console,'connection error:')); db.once('open',function () { console.log("connect db.") }); //模型 const todos = mongoose.model('todos',{ key: Number,todo: String }); //发送json: 数据+数量 let sendjson = { data:[],count:0 }; //设置跨域访问 app.all('*',function(req,res,next) { res.header("Access-Control-Allow-Origin","*"); res.header("Access-Control-Allow-Headers","Origin,X-Requested-With,Content-Type,Accept"); res.header("Access-Control-Allow-Methods","PUT,POST,GET,DELETE,OPTIONS"); res.header("X-Powered-By",' 3.2.1'); res.header("Content-Type","application/json;charset=utf-8"); next(); }); //api/todos app.post('/api/todos',res){ const p1 = todos.find({}) .exec((err,result) => { if (err) console.log(err); else { sendjson['data'] = result; } }); const p2 = todos.count((err,result) => { if(err) console.log(err); else { sendjson['count'] = result; } }).exec(); Promise.all([p1,p2]) .then(function (result) { console.log(result); res.send(JSON.stringify(sendjson)); }); }); //api/todos/add app.post('/api/todos/add',res){ todos.create(req.body,function (err) { res.send(JSON.stringify({status: err? 0 : 1})); }) }); //api/todos/remove app.post('/api/todos/remove',res){ todos.remove(req.body,function (err) { res.send(JSON.stringify({status: err? 0 : 1})); }) }); //设置监听80端口 app.listen(80,function () { console.log('listen *:80'); });
"scripts": { "start": "node scripts/start.js","build": "node scripts/build.js","test": "node scripts/test.js --env=jsdom","server": "node server.js" }, Ⅲ. Fetch后台数据 @action fetchTodos(){ fetch('http://localhost/api/todos',{ method:'POST',headers: { "Content-type":"application/json" },body: JSON.stringify({ current: this.current,pageSize: this.pageSize }) }) .then((response) => { // console.log(response); response.json().then(function(data){ console.log(data); this.total = data.count; this._key = data.data.length===0 ? 0: data.data[data.data.length-1].key; this.todos = data.data; this.loading = false; }.bind(this)); }) .catch((err) => { console.log(err); }) } @action fetchTodoAdd(){ fetch('http://localhost/api/todos/add',body: JSON.stringify({ key: this._key,todo: this.newtodo,}) }) .then((response) => { // console.log(response); response.json().then(function(data){ console.log(data); /*成功添加 总数加1 添加失败 最大_key恢复原有*/ if(data.status){ this.total += 1; this.todos.push({ key: this._key,}); message.success('添加成功!'); }else{ this._key -= 1; message.error('添加失败!'); } }.bind(this)); }) .catch((err) => { console.log(err); }) } @action fetchTodoRemove(keyArr){ fetch('http://localhost/api/todos/remove',body: JSON.stringify({ key: keyArr }) }) .then((response) => { console.log(response); response.json().then(function(data){ // console.log(data); if(data.status){ if(keyArr.length > 1) { this.todos = this.todos.filter(item => this.selectedRowKeys.indexOf(item.key) === -1); this.selectedRowKeys = []; }else{ this.todos = this.todos.filter(item => item.key !== keyArr[0]); } this.total -= keyArr.length; message.success('删除成功!'); }else{ message.error('删除失败!'); } }.bind(this)); }) .catch((err) => { console.log(err); }) } 注意
截图
源码Github (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |