Commit 55badd49 authored by huanglu's avatar huanglu

小程序模板项目集成dva

parent f20b202c
const child_process = require('child_process'); const child_process = require('child_process');
child_process.fork('build/watch.js', { const wacthProcess = child_process.fork('build/watch.js', {
cwd: process.cwd(), cwd: process.cwd(),
stdio: 'inherit' stdio: 'inherit',
}) })
child_process.spawn('npm', ['run', 'dev:weapp'], { const devProcess = child_process.spawn('npm', ['run', 'dev:weapp'], {
cwd: process.cwd(), cwd: process.cwd(),
stdio: 'inherit' stdio: 'inherit'
}) })
devProcess.on('close', () => {
process.exit(0)
})
\ No newline at end of file
...@@ -3,7 +3,7 @@ const fs = require('fs') ...@@ -3,7 +3,7 @@ const fs = require('fs')
const { autoInjectModels } = require('./models') const { autoInjectModels } = require('./models')
const { resolve } = require('./util') const { resolve } = require('./util')
console.log('watch') console.log('正在监听文件修改...')
fs.watch(resolve('src'), { persistent: true, interval: 5 , recursive: true }, (curr, filename) => { fs.watch(resolve('src'), { persistent: true, interval: 5 , recursive: true }, (curr, filename) => {
if ((filename || '').indexOf('model') != -1) { if ((filename || '').indexOf('model') != -1) {
......
...@@ -16,3 +16,37 @@ declare const process: { ...@@ -16,3 +16,37 @@ declare const process: {
[key: string]: any; [key: string]: any;
} }
} }
type Action<T> = {
type: string,
payload?: T
}
type Dispatch = ({
type: string,
payload: any
}) => Promise<any>
type Reducer<T> = (state: T, action: Action<T>) => T
type EffectsCommandMap = {
put: <A extends Action<any>>(action: A) => any,
call: Function,
select: Function,
take: Function,
cancel: Function,
[key: string]: any,
}
type Effect = (action: Action<any>, effects: EffectsCommandMap) => void
type DvaModel<T> = {
namespace: string,
state: T,
reducers?: {
[key: string]: Reducer<T>
},
effects?: {
[key: string]: Effect
}
}
...@@ -32,6 +32,7 @@ ...@@ -32,6 +32,7 @@
"@tarojs/cli": "^2.2.3", "@tarojs/cli": "^2.2.3",
"@tarojs/components": "2.2.3", "@tarojs/components": "2.2.3",
"@tarojs/components-qa": "2.2.3", "@tarojs/components-qa": "2.2.3",
"@tarojs/redux": "^2.2.3",
"@tarojs/router": "2.2.3", "@tarojs/router": "2.2.3",
"@tarojs/taro": "2.2.3", "@tarojs/taro": "2.2.3",
"@tarojs/taro-alipay": "2.2.3", "@tarojs/taro-alipay": "2.2.3",
...@@ -47,11 +48,13 @@ ...@@ -47,11 +48,13 @@
"dva-loading": "^3.0.20", "dva-loading": "^3.0.20",
"nerv-devtools": "^1.5.5", "nerv-devtools": "^1.5.5",
"nervjs": "^1.5.5", "nervjs": "^1.5.5",
"redux": "^4.0.5",
"regenerator-runtime": "0.11.1" "regenerator-runtime": "0.11.1"
}, },
"devDependencies": { "devDependencies": {
"@tarojs/mini-runner": "2.2.3", "@tarojs/mini-runner": "2.2.3",
"@tarojs/webpack-runner": "2.2.3", "@tarojs/webpack-runner": "2.2.3",
"@types/dva": "^1.1.0",
"@types/react": "^16.4.6", "@types/react": "^16.4.6",
"@types/webpack-env": "^1.13.6", "@types/webpack-env": "^1.13.6",
"@typescript-eslint/eslint-plugin": "^2.13.0", "@typescript-eslint/eslint-plugin": "^2.13.0",
......
import Taro, { Component, Config } from "@tarojs/taro"; import Taro, { Component, Config } from "@tarojs/taro";
import Index from "./pages/index"; import Index from "./pages/index";
import dva from 'dva-core' import dva from '@/utils/dva'
import { Provider } from '@tarojs/redux'
import models from '@/models' import models from '@/models'
import "./app.scss"; import "./app.scss";
const app = dva.createApp({
models
})
const store = app.getStore()
class App extends Component { class App extends Component {
/** /**
* 指定config的类型声明为: Taro.Config * 指定config的类型声明为: Taro.Config
...@@ -30,9 +36,9 @@ class App extends Component { ...@@ -30,9 +36,9 @@ class App extends Component {
// 请勿修改此函数 // 请勿修改此函数
render() { render() {
return ( return (
<StoreProvider store={store}> <Provider store={store}>
<Index /> <Index />
</StoreProvider> </Provider>
) )
} }
} }
......
import { action, Action } from 'easy-peasy'
type CommonModels = { type CommonModels = {
count: number, count: number
addCount: Action<CommonModels, number>;
} }
const commonModels: CommonModels = { export default <DvaModel<CommonModels>> {
count: 0, namespace: 'common',
addCount: action((state, payload) => { state: {
state.count = payload count: 0
},
reducers: {
add (state, action) {
console.log(state, action);
state.count += 1;
return {
...state
}
}
},
effects: {
*good({ payload }, { call, put }) {
console.log(payload, call)
yield new Promise((r) => {
setTimeout(() => {
r()
}, 2000)
}) })
yield put({
type: 'add'
})
}
}
} }
export default commonModels
\ No newline at end of file
/* Auto import */ /* Auto import */
import home from "@/pages/home/model.ts"; import home from "@/pages/home/model.ts";
import common from "@/models/common.ts"; import common from "@/models/common.ts";
export default [ export default [
home, home,
common, common,
......
import { FunctionComponent } from "@tarojs/taro"; import { FunctionComponent } from "@tarojs/taro";
import { View } from "@tarojs/components"; import { View, Button } from "@tarojs/components";
import styles from './index.scss' import styles from './index.scss'
import { connect } from "@tarojs/redux";
const Home: FunctionComponent<any> = ({ dispatch, home }) => {
const addCount = () => {
dispatch({
type: 'common/good'
})
}
const Home: FunctionComponent = () => {
return ( return (
<View className={styles.home}> <View className={styles.home}>
this is home page { JSON.stringify(home) }
<Button onClick={() => {
addCount()
}}>sfsfs</Button>
</View> </View>
) )
} }
...@@ -14,4 +26,8 @@ Home.config = { ...@@ -14,4 +26,8 @@ Home.config = {
navigationBarTitleText: 'home' navigationBarTitleText: 'home'
} }
export default Home export default connect(({common}) => {
\ No newline at end of file return {
home: common
}
})(Home as any)
\ No newline at end of file
export default { type HomeModel = {
name: '', name: string
}
export default <DvaModel<HomeModel>>{
namespace: 'home',
state: {
name: 'huanglu'
}
} }
\ No newline at end of file
...@@ -2,9 +2,9 @@ import Taro from '@tarojs/taro'; ...@@ -2,9 +2,9 @@ import Taro from '@tarojs/taro';
import { create } from 'dva-core'; import { create } from 'dva-core';
import createLoading from 'dva-loading'; import createLoading from 'dva-loading';
let app; let app: any;
let store; let store: any;
let dispatch; let dispatch: Function;
let global: any = {} let global: any = {}
function createApp(opt) { function createApp(opt) {
......
This source diff could not be displayed because it is too large. You can view the blob instead.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment