提交 9d1eb122 编写于 作者: D dolymood

Merge branch 'dev' of github.com:didi/cube-ui into dev

...@@ -20,13 +20,18 @@ This module exports a function called `createAPI` with which you can invoke the ...@@ -20,13 +20,18 @@ This module exports a function called `createAPI` with which you can invoke the
- Example: - Example:
```js First we create Hello.vue component:
import Vue form 'vue'
import { createAPI } from 'cube-ui' ```vue
// import Cube from 'cube-ui' <template>
// const { createAPI } = Cube <div @click="clickHandler">
// the Vue component which needs to be instantiated in the api form {{content}}
const MyComponent = Vue.extend({ <slot name="other"></slot>
</div>
</template>
<script type="text/ecmascript-6">
export default {
name: 'hello', name: 'hello',
props: { props: {
content: { content: {
...@@ -34,24 +39,47 @@ This module exports a function called `createAPI` with which you can invoke the ...@@ -34,24 +39,47 @@ This module exports a function called `createAPI` with which you can invoke the
default: 'Hello' default: 'Hello'
} }
}, },
template: '<div @click="clickHandler">{{content}}<slot name="other"></slot></div>',
methods: { methods: {
clickHandler(e) { clickHandler(e) {
this.$emit('click', e) this.$emit('click', e)
} }
} }
}) }
// register the MyComponent so it can been invoked in api form </script>
createAPI(Vue, MyComponent, ['click'], true) ```
// invoke the Vue component in api from
Then we make Hello.vue to an API Style component by calling the `createAPI` method.
```js
import Vue from 'vue'
import Hello from './Hello.vue'
import createAPI from 'cube-ui/lib/create-api'
// import Style to load the base style
import {
Style,
Dialog
} from 'cube-ui'
Vue.use(Dialog)
// create this.$createHello API
createAPI(Vue, Hello, ['click'], true)
// init Vue
new Vue({ new Vue({
el: '#app', el: '#app',
template: '<button @click="showHello">Show Hello</button>', render: function (h) {
return h('button', {
on: {
click: this.showHello
}
}, ['Show Hello'])
},
methods: { methods: {
showHello() { showHello() {
/* /* The first parameter of `$createHello` will be passed to the component as its props except the events in `events`(It will transform by default, eg: If `events` has value `['click']`, then the prop `onClick` will be treated as component's event and not component's props) */
The first parameter of `$createHello` will be passed to the component as its props except the events in `events`(It will transform by default, eg: If `events` has value `['click']`, then the prop `onClick` will be treated as component's event and not component's props)
*/
const instance = this.$createHello({ const instance = this.$createHello({
content: 'My Hello Content', content: 'My Hello Content',
onClick(e) { onClick(e) {
...@@ -66,13 +94,22 @@ This module exports a function called `createAPI` with which you can invoke the ...@@ -66,13 +94,22 @@ This module exports a function called `createAPI` with which you can invoke the
}) })
// Also, the event hanlder can be registered by instance's `$on` method // Also, the event hanlder can be registered by instance's `$on` method
instance.$on('click', (e) => { instance.$on('click', (e) => {
console.log('on click', e) const $dialog = this.$createDialog({
type: 'confirm',
content: 'click confirm to remove current instance',
icon: 'cubeic-alert'
})
$dialog.show()
$dialog.$on('confirm', () => {
// remove instance
instance.remove()
}).$on('cancel', () => {
console.log('cancel')
})
}) })
// destroy the component and detach the component's content from `body` element
instance.remove()
} }
} }
}) })
``` ```
In this example, we create a component `Hello` which needs to be invoked in api form and we invoke it in another component.The focus is what `showHello()` does: invoking method `this.$createHello(config, renderFn)` to instantiate `Hello`.
In this example, we create a component `MyComponent` with its name option `hello` which needs to be invoked in api form and we invoke it in another component.The focus is what `showHello()` does: invoking method `this.$createHello(config, renderFn)` to instantiate `MyComponent`.
...@@ -20,14 +20,18 @@ ...@@ -20,14 +20,18 @@
- 示例: - 示例:
```js 我们先编写一个 Hello.vue 组件:
import Vue form 'vue'
// 得到 createAPI ```vue
import { createAPI } from 'cube-ui' <template>
// or import Cube from 'cube-ui' <div @click="clickHandler">
// const { createAPI } = Cube {{content}}
// 需要提供 API 方式实例化的组件 <slot name="other"></slot>
const MyComponent = Vue.extend({ </div>
</template>
<script type="text/ecmascript-6">
export default {
name: 'hello', name: 'hello',
props: { props: {
content: { content: {
...@@ -35,19 +39,44 @@ ...@@ -35,19 +39,44 @@
default: 'Hello' default: 'Hello'
} }
}, },
template: '<div @click="clickHandler">{{content}}<slot name="other"></slot></div>',
methods: { methods: {
clickHandler(e) { clickHandler(e) {
this.$emit('click', e) this.$emit('click', e)
} }
} }
}) }
// 调用 </script>
createAPI(Vue, MyComponent, ['click'], true) ```
// 在其他组件中使用
然后我们再通过 `createAPI` 把 Hello.vue 变成一个 API 式调用的组件并调用。
```js
import Vue from 'vue'
import Hello from './Hello.vue'
import createAPI from 'cube-ui/lib/create-api'
// 引入 Style 加载基础样式
import {
Style,
Dialog
} from 'cube-ui'
Vue.use(Dialog)
// 创建 this.$createHello API
createAPI(Vue, Hello, ['click'], true)
// 初始化 Vue
new Vue({ new Vue({
el: '#app', el: '#app',
template: '<button @click="showHello">Show Hello</button>', render: function (h) {
return h('button', {
on: {
click: this.showHello
}
}, ['Show Hello'])
},
methods: { methods: {
showHello() { showHello() {
// 直接调用 // 直接调用
...@@ -68,13 +97,23 @@ ...@@ -68,13 +97,23 @@
}) })
// 通过 Vue 组件的 $on 也是可以监听的,看使用场景 // 通过 Vue 组件的 $on 也是可以监听的,看使用场景
instance.$on('click', (e) => { instance.$on('click', (e) => {
console.log('on click', e) const $dialog = this.$createDialog({
type: 'confirm',
content: '点击确定关闭当前实例',
icon: 'cubeic-alert'
})
$dialog.show()
$dialog.$on('confirm', () => {
// 销毁实例
instance.remove()
}).$on('cancel', () => {
console.log('cancel')
})
}) })
// 移除销毁
instance.remove()
} }
} }
}) })
``` ```
示例中就是创建了一个需要 API 调用的组件 `MyComponent`,名字为 `hello`,然后在其他组件中去使用,重点就是 `showHello()` 方法做的事情:调用 `this.$createHello(config, renderFn)` 实现组件的实例化。 示例中就是创建了一个需要 API 调用的组件 `Hello`,然后在其他组件中去使用,重点就是 `showHello()` 方法做的事情:调用 `this.$createHello(config, renderFn)` 实现组件的实例化。
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册