vue-create-api
A Vue plugin which make Vue component invocated by API.
Installing
use npm
$ npm install vue-create-api
use cdn
<script src="https://unpkg.com/vue-create-api/dist/vue-create-api.min.js"></script>
Usage
Vue// or with options.Vue// then the Vue constructor will have the createAPI function.// make Dialog component invocated by API.Vue// use in general JS files.// however, the $props can not be reactive.Dialog// use in a vue component.this
// typescript Vue.useCreateAPI Vue.createAPIDialog, events, single this.$createDialog.show
// d.ts; declare declare
Tip
using typescript,
terser-webpack-plugin
(vue-cli3.x) oruglifyjs
(vue-cli2.x) adds{ keep_fnames: true }
Constructor Options
key | description | default |
---|---|---|
componentPrefix |
the prefix name of your component | - |
apiPrefix |
the api prefix | $create |
Methods
Vue.createAPI(Component, [single])
-
Parameters:
{Function | Object} Component
Vue component which must containsname
{Boolean} [single]
whether singleton
-
Usage:
-
This method will add a method which is named
$create{camelize(Component.name)}
to Vue's prototype, so you can instantiate the Vue component byconst instance = this.$createAaBb(config, [renderFn, single])
in other components. The instantiated component's template content will be attached tobody
element. -
const instance = this.$createAaBb(config, renderFn, single)
Parameters:
Attribute Description Type Accepted Values Default config Config options Object {} - renderFn Optional, used to generate the VNode child node in the slot scene in general Function - function (createElement) {...} single Optional, whether the instantiated component is a singleton or not. If two parameters are provided and the renderFn
's type is not function, then thesingle
value is the sencond parameter's value.Boolean true/false single in createAPI() Config options
config
:You can set
$props
and$events
inconfig
,$props
supported reactive properties, these props will be watched.Attribute Description Type Accepted Values Default $props Component props Object - {
title: 'title',
content: 'my content',
open: false
}$events Component event handlers Object - {
click: 'clickHandler',
select: this.selectHandler
}$props
example,{ [key]: [propKey] }
:title: 'title'content: 'my content'open: falsetitle
,content
andopen
are keys of the component prop or data, and the prop' value will be taken by the following steps:- If
propKey
is not a string value, then usepropKey
as the prop value. - If
propKey
is a string value and the caller instance dont have thepropKey
property, then usepropKey
as the prop value. - If
propKey
is a string value and the caller instance have thepropKey
property, then use the caller'spropKey
property value as the prop value. And the prop value will be reactive.
$events
example,{ [eventName]: [eventValue] }
:click: 'clickHandler'select: thisselectHandlerclick
andselect
are event names, and the event handlers will be taken by the following steps:- If
eventValue
is not a string value, then useeventValue
as the event handler. - If
eventValue
is a string value, then use the caller'seventValue
property value as the event handler.
You can set all avaliable properties in Vue, but you need to add prefix
$
, eg:thisThe Returned value
instance
:instance
is a instantiated Vue component.And the
remove
method will be attached to this instance.You can invoke the
remove
method to destroy the component and detach the component's content frombody
element.If the caller is destroyed and the
instance
will be automatically destroyed. - If
-
-
Example:
First we create Hello.vue component:
{{content}}Then we make Hello.vue as an API style component by calling the
createAPI
method.Vue// create this.$createHello APIVue// init Vueel: '#app'{return}methods:{const instance = this}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 whatshowHello()
does: invoking methodthis.$createHello(config, renderFn)
to instantiateHello
.
How to use in general JS files or use it in global
In vue component, you can call by this.$createHello(config, renderFn)
because the this
is just a Vue instance. But in general JS files, you need to use Hello.$create
. As shown below:
Vue // create this.$createHello and Hello.create APIVue Hello
Notice, when we use in general JS files, we can't make props be reactive.