方法一:

1、写插件:

在 src 文件夹下面建 lib 文件夹用于存放插件,lib 文件夹下再建toastr文件夹,在toastr文件夹下新建 toastr.js 和 toastr.vue两个文件。整个项目目录如下所示:

toastr.vue 的内容如下:

<template>
<div class="vue-toastr-wraper" v-show="isShow">
{{message}}
</div>
</template> <script>
export default {
name:'toastr',
props:{
message:{
default:"",
type:String
},
isShow:{
default:false,
type:Boolean
}
},
mounted(){
if(this.isShow){
setTimeout(() => {
this.isShow = false
},2500);
}
}
}
</script> <style scoped>
.vue-toastr-wraper{
background: rgba(0, 0, 0, 0.6);
color: #fff;
font-size: 17px;
padding: 10px;
border-radius:12px;
display: -webkit-box;
-webkit-box-pack: center;
-webkit-box-align: center;
position: fixed;
top: 50%;
left: 50%;
z-index: 2000;
-webkit-transform: translateY(-50%);
transform: translateY(-50%);
-webkit-transform: translateX(-50%);
transform: translateX(-50%);
}
</style>

toastr.vue 是做一个弹出提示,其中传入的参数有两个:message和 isShow,分别表示提示消息以及是否显示提示。

toastr.js中写install方法,内容如下:

import VueToastrPlugin from './toastr.vue'

const toastrPlugin = {
install: function(Vue) {
Vue.component(VueToastrPlugin.name, VueToastrPlugin)
}
} // global 情况下 自动安装
if (typeof window !== 'undefined' && window.Vue) {
window.Vue.use(toastrPlugin)
} // 导出模块
export default toastrPlugin

2. 本地测试

本插件的功能就这么多,因为我们尚未发布,所以先进行本地测试。

2.1 将 App.vue 多余代码删除。

2.2 在 main.js 中引入

import Vue from 'vue'
import App from './App.vue' import Toastr from './lib/toastr/toastr.js'
Vue.use(Toastr) new Vue({
el: '#app',
render: h => h(App)
})

2.3 在 App.vue 中使用 toastr (别忘记需要传递的参数)

<template>
<div id="app">
<toastr :msg = "'测试'" :isShow = "true"/>
</div>
</template> <script>
export default {
name: 'app'
}
</script> <style lang="scss">
</style>

方法二:

1、写插件:

在 src 文件夹下面建 lib 文件夹用于存放插件,lib 文件夹下再建toastr文件夹,在toastr文件夹下新建 toastr.js 和 toastr.vue两个文件。整个项目目录如下所示:

toastr.vue 的内容如下:

<template>
<transition name="fade">
<div class="toast" v-show="show">
{{message}}
</div>
</transition>
</template> <script>
export default {
data() {
return {
show: false,
message: ""
};
}
};
</script> <style lang="scss" scoped>
.toast {
position: fixed;
top: 50%;
left: 50%;
margin-left: -100px;
margin-top: -30px;
padding: 2vw;
width: 200px;
height: 60px;
overflow-y: auto;
font-size: 14px;
color: #fff;
text-align: left;
background-color: rgba(0, 0, 0, 0.8);
border-radius: 3px;
z-index: 999;
} .fade-enter-active,
.fade-leave-active {
transition: 0.3s ease-out;
}
.fade-enter {
opacity: 0;
transform: scale(1.2);
}
.fade-leave-to {
opacity: 0;
transform: scale(0.8);
}
</style>

toastr.js中写install方法,内容如下:

import ToastComponent from './toastr.vue'

const Toast = {};

// 注册Toast
Toast.install = function (Vue) {
// 生成一个Vue的子类
// 同时这个子类也就是组件
const ToastConstructor = Vue.extend(ToastComponent)
// 生成一个该子类的实例
const instance = new ToastConstructor(); // 将这个实例挂载在我创建的div上
// 并将此div加入全局挂载点内部
instance.$mount(document.createElement('div'))
document.body.appendChild(instance.$el) // 通过Vue的原型注册一个方法
// 让所有实例共享这个方法
Vue.prototype.$toast = (msg, duration = 2000) => {
instance.message = msg;
instance.show = true; setTimeout(() => { instance.show = false;
}, duration);
}
} export default Toast

2. 本地测试

2.1 在 main.js 中引入:

// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import Toast from './lib/toastr/toastr.js' Vue.use(Toast) /* eslint-disable no-new */
new Vue({
el: '#app',
router,
store,
components: { App },
template: '<App/>'
})

2.2 在 App.vue 中使用 toastr:

// app.vue
<template>
<div id="app">
<button @click="toast">显示taost弹出框</button>
</div>
</template> <script>
export default {
name: "app",
methods: {
toast() {
this.$toast("你好");
}
}
};
</script> <style>
#app {
font-family: "Avenir", Helvetica, Arial, sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
text-align: center;
color: #2c3e50;
margin-top: 60px;
}
</style>

比较:

以上介绍了这两种不同的插件编写方法,貌似没有什么不一样啊,真的是这样么?

来看一下以上完整的main.js和app.vue两个文件发现,第一个toastr是显示的写在app.vue模板里的,而第二个toastr并没有作为一个组件写入,仅仅是通过一个方法控制显示。

vue插件开发的两种方法:以通知插件toastr为例的更多相关文章

  1. jQuery插件开发的两种方法及$.fn.extend的详解(转)

    jQuery插件开发的两种方法及$.fn.extend的详解 jQuery插件开发分为两种:1 类级别.2 对象级别,下面为大家详细介绍下   jQuery插件开发分为两种: 1 类级别 类级别你可以 ...

  2. jQuery插件开发的两种方法及$.fn.extend的详解

    jQuery插件开发分为两种: 1 类级别 类级别你可以理解为拓展jquery类,最明显的例子是$.ajax(...),相当于静态方法. 开发扩展其方法时使用$.extend方法,即jQuery.ex ...

  3. jQuery插件开发的两种方法

    1 类级别 类级别你可以理解为拓展jquery类,最明显的例子是$.ajax(...),相当于静态方法. 开发扩展其方法时使用$.extend方法,即jQuery.extend(object); $. ...

  4. 解决vue项目eslint校验 Do not use 'new' for side effects 的两种方法

    import Vue from 'vue' import App from './App.vue' import router from './router' new Vue({ el: '#app' ...

  5. Vue 创建组件的两种方法

    地址:https://blog.csdn.net/cofecode/article/details/74634301 创建组件的两种方法 1.全局注册 2.局部注册 var child=Vue.ext ...

  6. 两种方法使vue实现jQuery调用

    引言 如果说vue是前端工程化使用较多的骨架,那么JavaScript就是我们的前端的细胞.MVVM模式让我们体验到前端开发的便携,无需再过多的考虑DOM的操作.而vue的渐进式开发(逐步引用组件,按 ...

  7. vue里使用element饿了么的el-menu+vue-router实现路由跳转的两种方法

    最近准备写一个echarts的可视化展示案例,首先用vue-cli3创建了一个项目(好像vue-cli4也出来,感觉变化不大,就没升级了) 然后,开始配置路由↓下面是我的router.js文件 imp ...

  8. vue中使用echarts的两种方法

    在vue中使用echarts有两种方法一.第一种方法1.通过npm获取echarts npm install echarts --save 2.在vue项目中引入echarts 在 main.js 中 ...

  9. 安卓ListView操作的两种方法

    举例做一个微信的中间部分(好友消息等信息通知) 第一种:BaseAdapter() package com.example.wx; import java.util.ArrayList;import ...

随机推荐

  1. 2019年9月训练(壹)数位DP (HDU 2089)

    开学之后完全没时间写博客.... HDU 2089 不要62(vjudge) 数位DP 思路: 题目给出区间[n,m] ,找出不含4或62的数的个数 用一个简单的差分:先求0~m+1的个数,再减去0~ ...

  2. 12306火车票余票查询&Python实现邮件发送

    查询余票接口 打开12306官网,并进入余票查询页面,同时开启chrome浏览器F12控制台,以北京到上海为例,搜索2018年10月1日的余票信息,点击搜索按钮,可以在控制台发送了一条GET请求,请求 ...

  3. php 连接sqlserver

    本地环境windows 10+phpstudy2016+ SQL Server 2008 R2 x86+php7.0查看自己sql server 多少位可以在新建查询里输入 select @@VERS ...

  4. 从入门到自闭之Python集合,深浅拷贝(大坑)

    小数据池 int: -5~256 str: 字母,数字长度任意符合驻留机制 字符串进行乘法时总长度不能超过20 特殊符号进行乘法时只能乘以0 代码块: 一个py文件,一个函数,一个模块,终端中的每一行 ...

  5. Socket-实例

    import socket,os,time server = socket.socket() server.bind(("localhost",9999)) server.list ...

  6. windows 的文件夹映射实现

    具体的操作命令如下:MKLINK [[/D] | [/H] | [/J]] Link Target/D:创建目录符号链接.默认为文件符号链接./H:创建硬链接,而不是符号链接./J:创建目录联接.Li ...

  7. PowerBI 实现不同角色看到内容不同支持动态权限管理

    首先,在PowerBIDesktop中进行设计,先设计一个权限表: 具体权限如下: 也就是说,这些用户账号在PowerBIService登录时,会分别代表这些用户,接下来会使用一个很重要的动态函数:U ...

  8. 如何用Mvc实现一个列表页面-异步加载

    在接触Mvc后,开始有点觉得很累,什么都要自己做,完全没有WebForm的易用性: 大概用了一个月左右的时候,越用用顺手,就习惯了MVC的这种开发方式,灵活,简洁: 当初学习MVC,网上看资料,都是讲 ...

  9. Vue-CLI项目汇总

    Vue-CLI 项目搭建 Vue-CLI 项目在pycharm中配置 Vue-CLI 项目中相关操作 Vue-CLI项目中路由传参 Vue-CLI项目-vue-cookie与vue-cookies处理 ...

  10. DPI,像素,英寸的关系

    DPI * 英寸 = 像素 eg:在150dpi打印 (2 x 4)英寸的照片 等到多少像素的图像 (150*2)x (150*4) =300 x 600像素 同理可得  已知像素.英寸大小 求DPI ...