Commit 55badd49 authored by huanglu's avatar huanglu

小程序模板项目集成dva

parent f20b202c
const child_process = require('child_process');
child_process.fork('build/watch.js', {
const wacthProcess = child_process.fork('build/watch.js', {
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(),
stdio: 'inherit'
})
devProcess.on('close', () => {
process.exit(0)
})
\ No newline at end of file
......@@ -3,7 +3,7 @@ const fs = require('fs')
const { autoInjectModels } = require('./models')
const { resolve } = require('./util')
console.log('watch')
console.log('正在监听文件修改...')
fs.watch(resolve('src'), { persistent: true, interval: 5 , recursive: true }, (curr, filename) => {
if ((filename || '').indexOf('model') != -1) {
......
......@@ -16,3 +16,37 @@ declare const process: {
[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 @@
"@tarojs/cli": "^2.2.3",
"@tarojs/components": "2.2.3",
"@tarojs/components-qa": "2.2.3",
"@tarojs/redux": "^2.2.3",
"@tarojs/router": "2.2.3",
"@tarojs/taro": "2.2.3",
"@tarojs/taro-alipay": "2.2.3",
......@@ -47,11 +48,13 @@
"dva-loading": "^3.0.20",
"nerv-devtools": "^1.5.5",
"nervjs": "^1.5.5",
"redux": "^4.0.5",
"regenerator-runtime": "0.11.1"
},
"devDependencies": {
"@tarojs/mini-runner": "2.2.3",
"@tarojs/webpack-runner": "2.2.3",
"@types/dva": "^1.1.0",
"@types/react": "^16.4.6",
"@types/webpack-env": "^1.13.6",
"@typescript-eslint/eslint-plugin": "^2.13.0",
......
import Taro, { Component, Config } from "@tarojs/taro";
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 "./app.scss";
const app = dva.createApp({
models
})
const store = app.getStore()
class App extends Component {
/**
* 指定config的类型声明为: Taro.Config
......@@ -30,9 +36,9 @@ class App extends Component {
// 请勿修改此函数
render() {
return (
<StoreProvider store={store}>
<Provider store={store}>
<Index />
</StoreProvider>
</Provider>
)
}
}
......
import { action, Action } from 'easy-peasy'
type CommonModels = {
count: number,
addCount: Action<CommonModels, number>;
count: number
}
const commonModels: CommonModels = {
count: 0,
addCount: action((state, payload) => {
state.count = payload
})
export default <DvaModel<CommonModels>> {
namespace: 'common',
state: {
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 */
import home from "@/pages/home/model.ts";
import common from "@/models/common.ts";
export default [
home,
common,
......
import { FunctionComponent } from "@tarojs/taro";
import { View } from "@tarojs/components";
import { View, Button } from "@tarojs/components";
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 (
<View className={styles.home}>
this is home page
{ JSON.stringify(home) }
<Button onClick={() => {
addCount()
}}>sfsfs</Button>
</View>
)
}
......@@ -14,4 +26,8 @@ Home.config = {
navigationBarTitleText: 'home'
}
export default Home
\ No newline at end of file
export default connect(({common}) => {
return {
home: common
}
})(Home as any)
\ No newline at end of file
export default {
name: '',
type HomeModel = {
name: string
}
export default <DvaModel<HomeModel>>{
namespace: 'home',
state: {
name: 'huanglu'
}
}
\ No newline at end of file
......@@ -2,13 +2,13 @@ import Taro from '@tarojs/taro';
import { create } from 'dva-core';
import createLoading from 'dva-loading';
let app;
let store;
let dispatch;
let app: any;
let store: any;
let dispatch: Function;
let global: any = {}
function createApp(opt) {
app = create(opt);
app.use(createLoading({}));
......
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