quick-start.md 1.8 KB
Newer Older
M
init  
miaodian 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
## 快速上手

### 安装

```shell
$ npm install cube-ui --save
```

### 使用

推荐使用 [babel-plugin-transform-modules](https://www.npmjs.com/package/babel-plugin-transform-modules) 插件,可以更优雅引入组件模块以及对应的样式。

但在使用之前,需要配置下这个插件,修改 .babelrc:

- webpack 1.x
  ```json
  {
    "plugins": ["transform-modules", {
      "cube-ui": {
        "transform": "cube-ui/lib/${member}",
        "kebabCase": true,
        "style": true
      }
    }]
  }
  ```
- webpack 2+
  ```json
  {
    "plugins": ["transform-modules", {
      "cube-ui": {
        "transform": "cube-ui/src/modules/${member}",
        "kebabCase": true
      }
    }]
  }
  ```

> [为何要区分 webpack 版本?](#/zh-CN/docs/post-compile)

如果不使用 babel-plugin-transform-modules 插件的话,需要手工引入对应的样式文件。

#### 全部引入

一般在入口文件中:

```javascript
import Vue from 'vue'
import Cube from 'cube-ui'

Vue.use(Cube)
```

#### 按需引入

```javascript
import { Button } from 'cube-ui'
```

你可以选择全局注册也可以选择局部注册:

```js
// 全局注册
Vue.use(Button)

// 或者局部注册
// 某个组件中
{
  components: {
    CubeButton: Button
  }
}
```

所有的可按需引入的组件:

```js
import {
  Button,
  Checkbox,
  Loading,
  Tip,
  Toast,
  Picker,
  TimePicker,
  Dialog,
  ActionSheet,
  Scroll,
  Slide,
  IndexList
} from 'cube-ui'
```

#### 示例

```html
<template>
  <cube-button @click="showDiaog">show dialog<cube-button>
</template>

<script>
  export default {
    methods: {
      showDialog() {
        this.$createDialog({
          type: 'alert',
          title: 'Alert',
          content: 'dialog content'
        }).show()
      }
    }
  }
</script>
```