Vue 开发插件

Vue.js 的插件有一个公开方法 install,这个方法的第一个参数是 Vue 构造器,第二个参数是一个可选的选项对象。

Dialog.vue

<template>  
    <div class="help-dialog">        
        <v-dialog v-model="dialog" width="500">            
            <v-card>                
                <v-card-title class="headline lighten-2" primary-title>{{ title }}</v-card-title>                
                    <v-card-text>{{ content }}</v-card-text>                
                    <v-divider></v-divider>                
                    <v-card-actions>                    
                        <v-spacer></v-spacer>                    
                        <v-btn flat color="primary">{{ activatorTxt }}</v-btn>                    
                        <v-spacer></v-spacer>                
                    </v-card-actions>            
            </v-card>        
        </v-dialog>  
    </div>
</template>

<script>
    export default {        
        name: 'HelpDialog',        
        data () {            
            return {                
                dialog: false,                
                title: 'Tips',                
                content: 'Here is the content.',                
                activatorTxt: 'confirm'            
            }        
        }    
    }
</script>复制代码


index.js

import HelpDialogComponent from './HelpDialog'
const HelpDialog = {    
    install(Vue) {        
        const HelpDialogConstructor = Vue.extend(HelpDialogComponent)        
        const instance = new HelpDialogConstructor()
        instance.$mount(document.createElement('div'))        
        document.body.append(instance.$el)
        Vue.prototype.$helpDialog = function(params = {                 
            title: 'Tips',                 
            content: 'Here is the content.'             
        }) {            
                instance.dialog = true            
                instance.title = params.title            
                instance.content = params.content            
                instance.activatorTxt = '确认'        
            }    
    }
}
export default HelpDialog复制代码


main.js 

import Vue from 'vue'
import HelpDialog from '@/components/index'Vue.use(HelpDialog)复制代码


usage

Vue.prototype.$helpDialog({    
    title: 'Tips',    
    content: 'Here is the content.'
})复制代码


转载于:https://juejin.im/post/5c480b85e51d453ba8103e1f

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值