如何使用

修改

创建项目

使用路由装饰器创建路由,并返回json数据

api/controllers/any_controller.ts

1
2
3
4
5
6
7
8
9
10
11
var { Controller, Get } = require('koa-cola/client');

@Controller('/api')
export default class {
@Get('/todo/list')
list ( ) {
return [
'todo1', 'todo2'
]
}
}

使用路由装饰器创建路由,并通过react page组件渲染

渲染静态组件

api/controllers/any_controller.ts

1
2
3
4
5
6
7
8
var { Controller, Get, View } = require('koa-cola/client');

@Controller('/')
export default class {
@Get('index')
@View('index')
index ( ) {}
}

views/pages/index.tsx

1
2
3
4
5
6
import * as React from 'react';
export default function() {
return <div>
Wow koa-cola!
</div>
};

渲染数据依赖组件方法

api/controllers/any_controller.ts

1
2
3
4
5
6
7
8
var { Controller, Get, View } = require('koa-cola/client');

@Controller('/')
export default class {
@Get('index')
@View('index')
index async () {}
}

views/pages/index.tsx

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import * as React from 'react';
var { Cola } = require('koa-cola/client')

@Cola({
initData : {
foo : async ({ params, helpers }) => {
return await Promise.resolve('bar');
}
}
})
export default class Page extends React.Component<Props, States> {
constructor(props: Props) {
super(props);
}
render() {
return <div>
<div>{this.props.foo}</div>
</div>
}
};