Skip to content

Commit 41dfd8b

Browse files
committed
update doc and fix templdate
1 parent 01f67dd commit 41dfd8b

File tree

20 files changed

+415
-10
lines changed

20 files changed

+415
-10
lines changed

docs/book.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
],
88
"pluginsConfig": {
99
"github": {
10-
"url": "https://github.com/MMF-FE/vue-typescript/tree/develop"
10+
"url": "https://github.com/MMF-FE/vue-typescript"
1111
}
1212
},
1313
"links": {

docs/zh-cn/Api.md

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# 使用后端接口
2+
使用 [axios](https://github.com/mzabriskie/axios) 作为默认的 http client
3+
4+
## http.ts
5+
6+
**src/api/http.ts** 对 axios 进行了封装,目前只封装了 get, post 方法,需要其他 http 请求 method 可以自行封装。
7+
8+
### get
9+
10+
```typescript
11+
function get<T>(url, data?: Types.PlainObject): Promise<T> {
12+
}
13+
```
14+
**T** 是接口返回的数据类型
15+
16+
```typescript
17+
// 这里 /list 的接口返回的数据格式是 {status: number,list: any[]}
18+
{
19+
getList (params: any) {
20+
return get<{
21+
status: number
22+
list: any[]
23+
}>('/list', params)
24+
}
25+
}
26+
```
27+
28+
### post
29+
```typescript
30+
function post<T>(url, data?: Types.PlainObject): Promise<T> {
31+
}
32+
33+
```
34+
35+
## index.ts
36+
**src/api/index.ts** 是后端接口配置。可以将不同模块的接口拆分成几个文件。
37+
38+
## 在组件中使用 Api
39+
每个组件默认都有 **api** 这个成员,**api** 就是来自 src/api/index.ts
40+
41+
```typescript
42+
export default class Home extends Vue {
43+
async created () {
44+
// api example
45+
let res = await this.api.getList({})
46+
console.log(res.list)
47+
}
48+
}
49+
50+
```

docs/zh-cn/Build Command.md renamed to docs/zh-cn/Command.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ npm run svg
1818
```
1919

2020
## 添加组件
21-
使用 **./tools/cli.js** 添加组件。
21+
使用 **./tools/cli.js** 添加组件。组件模板在 tools/tpl, 你可以根据需要修改模板文件。
22+
2223
```text
2324
选项:
2425
--version, -v 显示版本号 [布尔]

docs/zh-cn/Component.md

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
# 组件
2+
本模板没有使用单文件组件,每个组件由四个文件组成,这是因为目前编辑器对 *.vue 文件的支持性不是很强。
3+
4+
## 组件结构
5+
6+
```
7+
├── hello.scss
8+
├── hello.ts
9+
├── hello.vue
10+
└── index.ts
11+
```
12+
13+
## vue 文件
14+
可以看到,组件模板是使用 vue 文件的,而不是使用 html, 这是因为可以借助 vue-loader 将单文件组件的优势融入进来。
15+
16+
```html
17+
<!-- vue 文件 -->
18+
<template>
19+
<div>
20+
<h1>Hello {{name}}</h1>
21+
</div>
22+
</template>
23+
<style src="./hello.scss" lang="scss" scoped></style>
24+
```
25+
26+
## ts 文件
27+
使用 typescript 编写组件,我们使用了 [vue-property-decorator](https://github.com/kaorun343/vue-property-decorator), 具体使用方式,请参考其文档。
28+
29+
```typescript
30+
import Vue from 'components/base'
31+
import { Component, Prop } from 'vue-property-decorator'
32+
import template from './hello.vue'
33+
34+
@Component({
35+
name: 'tag-hello',
36+
mixins: [template] // 使用 mixins 将 vue 文件的功能混合进来
37+
})
38+
export default class Hello extends Vue {
39+
// 定义 prop
40+
@Prop({ default: 'World' })
41+
name: string
42+
43+
// computed
44+
get fullname () {
45+
return `hello, ${this.name}`
46+
}
47+
48+
// method
49+
say () {
50+
console.log(this.fullname)
51+
}
52+
}
53+
54+
55+
```
56+
> 注意:[vue-property-decorator](https://github.com/kaorun343/vue-property-decorator) 的 Component 装饰器来自 [vue-class-component](https://github.com/vuejs/vue-class-component), 需要看一下该文档
57+
58+
## 样式
59+
请参考 [样式](/zh-cn/Style.md) 一节。
60+
61+
## index.ts
62+
这个文件是组件的入口文件,方便其他组件导入。
63+
64+
```typescript
65+
export * from './hello.ts'
66+
```
67+
68+
为什么不直接将 hello.ts 命名为 index.ts 呢?这是因为考虑到编辑器打开文件的时候显示文件名的问题,不然显示 index.ts 你都不知道这个是哪个组件。如果你不想修改 cli 的组件模板。
69+
70+
## 引入其他组件或模块
71+
引入其他组件或模块,默认以 src 目录开始查找的。
72+
73+
```js
74+
// 引入 'src/components/tags/hello'
75+
import Hello from 'components/tags/hello'
76+
```

docs/zh-cn/Issues.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# 存在的问题
2+
升级到 vue-loader 13.x 和 webpack3.x 遇到不少兼容问题。目前还没解决的有以下问题
3+
4+
### 修改 vue 文件或样式文件都会刷新浏览器
5+
这个目前定位到刷新的操作是在 build/dev-server.js 的一段代码。
6+
```js
7+
// force page reload when html-webpack-plugin template changes
8+
compiler.plugin('compilation', function (compilation) {
9+
compilation.plugin('html-webpack-plugin-after-emit', function (data, cb) {
10+
hotMiddleware.publish({ action: 'reload' })
11+
cb()
12+
})
13+
})
14+
```
15+
目前模板已经注释掉这段代码了,如果修改了 build/tpl 里面的 html, 需要手动刷新。
16+
17+
### production 模式下没有将样式抽离到 css 文件
18+
当 build production 的时候,组件里面的样式没有抽离到 css 文件中,这个问题目前还没有找到解决办法。
19+
20+
> 如果你找到了解决方法,麻烦提交一下 [issue](https://github.com/MMF-FE/vue-typescript/issues)[PR](https://github.com/MMF-FE/vue-typescript/pulls)

docs/zh-cn/README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
# 简介
22
vue-typescript 是在 [vuejs-templates/webpack](https://github.com/vuejs-templates/webpack) 的基础上修改的,主要是将开发语言从 ES6 改成 Typescript。使用本模板之前,建议看一下 [vuejs-templates/webpack 的文档](http://vuejs-templates.github.io/webpack/)
33

4+
## 版本
5+
```
6+
vue: 2.x
7+
vue-loader: 13.x
8+
webpack: 3.x
9+
typescript: 2.4+
10+
```
11+
412
## 开始
513
使用该模板,请先安装 [vue-cli](https://github.com/vuejs/vue-cli)
614

docs/zh-cn/SUMMARY.md

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
# Summary
22

33
* [简介](README.md)
4-
* [项目结构](Project Structure.md)
5-
* [命令](Build Command.md)
4+
* [项目结构](Structure.md)
5+
* [命令](Command.md)
6+
* [类型定义](/zh-cn/Types.md)
7+
* [组件](Component.md)
8+
* [使用后端接口](/zh-cn/Api.md)
9+
* [样式](/zh-cn/Style.md)
10+
* [Vuex](Vuex.md)
11+
* [测试](Test.md)
12+
* [存在的问题](Issues.md)
613

File renamed without changes.

docs/zh-cn/Style.md

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# 样式
2+
默认使用 sass(scss), 在 build/utils.js 配置如下:
3+
4+
```js
5+
scss: generateLoaders('sass', {
6+
includePaths: [
7+
path.join(__dirname, '../src/style'),
8+
path.join(__dirname, '../node_modules')
9+
]
10+
})
11+
```
12+
import sass 默认是从 src/style 目录或 node_modules 开始的, 所以即使你组件目录层次多深,引入样式都很方便。
13+
> 注意:如果是引入 node_modules 里面的模块,需要加上 **~**
14+
15+
```scss
16+
@import "base/base"; // 引入 src/style/base/_base.scss
17+
18+
@import "~normalize.css"; // 引入 node_modules/normalize.css
19+
```
20+
21+
## base.scss
22+
src/style/base/_base.scss 只能引入不能生成代码的模块,比如 变量、mixins 等。
23+
24+
## 资源路径(assets)
25+
sass 中引用资源(assets)使用 [postcss-assets](https://github.com/borodean/postcss-assets) 提供的 reslove 方法。默认的资源根路径是 src/assets/images。你可以修改 .postcssrc.js 文件来修改或添加路径。
26+
27+
```js
28+
// .postcssrc.js
29+
module.exports = {
30+
"plugins": {
31+
// to edit target browsers: use "browserlist" field in package.json
32+
// 浏览器配置在 package.json 那里编辑
33+
"autoprefixer": {},
34+
"postcss-assets": {
35+
relative: true,
36+
loadPaths: ['./src/assets/images']
37+
}
38+
}
39+
}
40+
```
41+
42+
```scss
43+
.logo {
44+
// url("src/assets/images/logo.png")
45+
// 页面显示的地址会被 webpack 处理过
46+
background-image: resolve("logo.png");
47+
}
48+
```
49+
50+
## scoped css
51+
创建组件的时候,默认样式开启 css scoped。
52+
53+
```html
54+
<style src="./home.scss" lang="scss" scoped></style>
55+
```
56+
css scoped 的好处是组件样式不会影响到其他组件或全局样式,也不会被其他组件(其他组件也是 scoped 的)影响,又不像 css module 那样需要修改 html 模板代码。如果你需要覆盖子组件的样式,可以使用 ** /deep/ **。具体例子可以参考 [scoped example](https://github.com/MMF-FE/vue-typescript/blob/master/template/src/components/views/scoped/scoped.scss)
57+
58+
```scss
59+
.parent {
60+
color: red;
61+
62+
/deep/ {
63+
.child {
64+
color: green;
65+
}
66+
}
67+
}
68+
```
69+
> 如果不使用 sass, 可以用 **>>>** 代替 **/deep/**
70+
> 由于使用 scoped css, 所以不需要嵌套太多层选择器,尽量不要嵌套。

docs/zh-cn/Test.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# 测试
2+
目前还没有加入单元测试。如果需要单元测试,请参考 [vuejs-webpack](https://github.com/vuejs-templates/webpack)

0 commit comments

Comments
 (0)