vue-mount
A tool for dynamic mounting Vue components and maintaining the component tree.
中文文档 | English
Demos
Table of contents
Getting Started
Install
You can install the library via npm.
npm i vue-mount -S
or via yarn:
yarn add vue-mount
or via CDN
<!-- import Vue. --> <!-- import vue-mount -->
Usage
Basic usage
;// just import a single file component; // mount component and return instanceconst alert = ;
Advanced usage
// The exposed method `mount(cmp, opt)` is the syntactic sugar of `new Mount(cmp, opt).mount()`;// alert.vue is a common `single file component`; const mountAlert = Alert // mount target. // the special value indicates create a new root vue instance target: 'new' // props data passed to component props: content: 'Mount alert component to a new Vue root instance' // modify component data data: someInnerData: 'modified' // attach event listeners on: 'some-event'eventData vm mnt // vm is a component instance // mnt is current Mount instance watch: content: immediate: true { console; // do unwatch // make sure the unwatch method exsit while `immediate` is true if mntunwatchMappercontent mntunwatchMappercontent; } ; // Get alert component instancelet alertVm = mountAlert;// Mount alert component// Component would not be mounted more than oncealertVm = mountAlert; // Dynamicly set props data of the component.mountAlert;// Destroy component and it's elementmountAlert; // New component would be mountedalertVm = mountAlert;
MountOption
target
- Type: { string | Element | Vue | VNode }
- Default:
new
- Details: You can pass
css selector
,Element
,Vue instance
,VNode
or special value includingnew
androot
new
: default value. Vue component would be mounted with a new Vue root instance.root
: Vue component would be mounted to an existing Vue root instance. if the root instance or root element is not found underMountOption.root
option, the component would be mounted with a new Vue root instance behaving just like optionnew
.- When giving a
Vue instance
, the component would replace/append the Vue instance and added to the components tree(see MountOption.mode), former Vue instance would be destroyed when the component is mounted.
- Examples:
Special Note: When configured as
new
, the mounted component cannot access the configuration passed in the form ofVue.prototype.$xxx
or when creating the root instance, resulting in the mounted component UNABLE to access configuration globally registered on the root component such asthis.$router
(because a new root instance was created); In other cases,vue-mount
will automatically query and join the component tree context.
mode
- Type: { string }
- Default:
replace
- Options:
replace
,append
- Details: Specific the mount mode. Corresponds to the behavior on its component tree.
- Examples:// Alert component would be append to current component;
Attention: When the value of option
target
isnew
, orroot
, optionmode
will be ignored and be reset toappend
.
root
- Type: { string | Element | Vue | VNode }
- Default:
#app
- Details: Specific a root element. (All values given will be parsed to be an HTML element)
rootOptions
- Type: { VueOptions }
- Default:
{}
- Details: Specific the Vue contructor options when creating new vue root instance
- Examples:
props
- Type: { Object }
- Details: Specific component props data.
data
- Type: { Object }
- Details: Modify component data after the component instance was created (mounted is not necessary).
on
- Type: { [event: string]: Function | Object }
- Details: Attach event listener to the component instance.
-
build-in event:
mount:mount
: Triggered when callingmount
method or ready to mount component。mount:destroy
: Triggered when (underlying) callingdestroy
method.
-
Object configure:
once
{ Boolean }: Whether the listener will be removed once it triggers for the first time.handler
{ Function }: The event callback function. Compared to event callback function of Vue (vm.$on/$once), the last two additional arguments are currentVue component
and currentMount instance
.
The
this
argument of the callback function points to the current Mount instance, although you can use the arrow function to avoid this behavior.
-
- Examples:
watch
- Type: { [key: string]: Function | Object }
- Details: An object where keys are expressions to watch and values are the corresponding callbacks. The value can also be a string of a method name, or an Object that contains additional options.
-
Object configure:
immediate
{ Boolean }: Passing inimmediate: true
in the option will trigger the callback immediately with the current value of the expression.deep
{ Boolean }: To also detect nested value changes inside Objects, you need to pass indeep: true
in the options argument.handler
{ Function }: The callback function when the value changes. Compared to the callback function of Vue (vm.$watch), there are always be 4 parameters like:newValue, oldValue, vm, mnt
. The last two additional arguments are currentVue component
and currentMount instance
.
The
this
argument of the callback function points to the current Mount instance, although you can use the arrow function to avoid this behavior. -
Unwatch: Each key you passed in the
watch
option will be added to the attributeunwatchMapper
of Mount instance, you can call the method likemnt.unwatchMapper.attr()
to unwatch it.
-
- Examples:
Attention: Only declare the data upfront in the
data
option, the callback function can be called.
Methods
getInstance(MountOptions)
- Arguments: { MountOptions }
- Returns: { Vue }
- Details: Return a vue component instance, calling the method multiple times will returns the same instance
Attention: When the value of option
target
isroot
while no root instance/element was found(which means need to create a new Vue instance), ortarget
isnew
, they all lead to the result that instance would be mounted right now.
In order to ensure behavioral consistency, it is recommended to use the
#mount
method first.
mount(MountOptions)
- Arguments: { MountOptions }
- Returns: { Vue }
- Details: Mount Vue component, update the component tree and return a Vue component instance.
Calling the method after the component was destroyed will re-mount the component.
Calling the method multiple times will ONLY mount once
set(MountDataOptions)
- Arguments: { MountDataOptions }
- Returns: { Mount } Current instance of
Mount
. - Details: Dynamicly set
props
,data
andlisteners
of the component.
destroy()
- Returns: { Vue }
- Details: Destroy the Vue component instance and remove the associated elements. Diff from Vue's $destroy method.
getDom()
- Returns: { Element | Node }
- Details: Returns the element associated with the component.
Methods added on components
$getMount()
- Returns: { VueMount }
- Details: returns the VueMount instance associated with the component instance.