有时候懒的把一些通用组件写到template
里面去,而业务中又须要用到,好比表示loading
状态这样组件。vue
若是是这样的组件,能够选择把组件手动初始化,让组件在整个app生命周期中始终保持活跃。api
如:app
// a.js import Vue from 'vue' import hello from './hello.vue' const wrapInstance = new Vue({ render(h) { return h(hello, {}) } }) const wrap = wrapInstance.$mount() // 渲染成DOM document.body.appendChild(wrap.$el) // 把DOM插入节点 const helloInstance = wrapInstance.$children[0] // 拿到的是当前的vue实例,hello实例是当前的子组件 export default helloInstance
// main.js import helloInstance from 'a.js' Vue.prototype.$someName = helloInstance
实例化一个vue组件,挂在到原型链 或者 项目root vue实例上,就能够经过函数式的调用组件的方法。在APP生命周期内能够永不摧毁,方便调用。函数
相似Element
组件库的loading组件 或者 message
组件。post
this.$message.error('错了哦,这是一条错误消息')
经过函数就能够调用Message
组件方法。this
在线实例
element文档地址.net
若是是一些全局性的组件,或者顶层组件,就能够考虑在生命周期永久实例化,绑定在VUE的原型上,方便开发的时候调用。prototype