插件
插件通常用來(lái)為 Vue 添加全局功能酿雪。插件的功能范圍沒(méi)有嚴(yán)格的限制——一般有下面幾種:
添加全局方法或者 property指黎。如:vue-custom-element
添加全局資源:指令/過(guò)濾器/過(guò)渡等州丹。如 vue-touch
通過(guò)全局混入來(lái)添加一些組件選項(xiàng)。如 vue-router
添加 Vue 實(shí)例方法亲怠,通過(guò)把它們添加到
Vue.prototype
上實(shí)現(xiàn)柠辞。一個(gè)庫(kù)钾腺,提供自己的 API,同時(shí)提供上面提到的一個(gè)或多個(gè)功能姻报。如 vue-router
使用插件
通過(guò)全局方法 Vue.use() 使用插件吴旋。它需要在你調(diào)用 new Vue() 啟動(dòng)應(yīng)用之前完成:
// 調(diào)用 `MyPlugin.install(Vue)`
Vue.use(MyPlugin)
new Vue({
// ...組件選項(xiàng)
})
也可以傳入一個(gè)可選的選項(xiàng)對(duì)象:
Vue.use(MyPlugin, { someOption: true })
Vue.use 會(huì)自動(dòng)阻止多次注冊(cè)相同插件厢破,屆時(shí)即使多次調(diào)用也只會(huì)注冊(cè)一次該插件摩泪。
Vue.js 官方提供的一些插件 (例如 vue-router) 在檢測(cè)到 Vue 是可訪問(wèn)的全局變量時(shí)會(huì)自動(dòng)調(diào)用 Vue.use()。然而在像 CommonJS 這樣的模塊環(huán)境中嚷掠,你應(yīng)該始終顯式地調(diào)用 Vue.use():
// 用 Browserify 或 webpack 提供的 CommonJS 模塊環(huán)境時(shí)
var Vue = require('vue')
var VueRouter = require('vue-router')
// 不要忘了調(diào)用此方法
Vue.use(VueRouter)
awesome-vue 集合了大量由社區(qū)貢獻(xiàn)的插件和庫(kù)不皆。
開(kāi)發(fā)插件
Vue.js 的插件應(yīng)該暴露一個(gè) install 方法熊楼。這個(gè)方法的第一個(gè)參數(shù)是 Vue 構(gòu)造器鲫骗,第二個(gè)參數(shù)是一個(gè)可選的選項(xiàng)對(duì)象:
MyPlugin.install = function (Vue, options) {
// 1. 添加全局方法或 property
Vue.myGlobalMethod = function () {
// 邏輯...
}
// 2. 添加全局資源
Vue.directive('my-directive', {
bind (el, binding, vnode, oldVnode) {
// 邏輯...
}
...
})
// 3. 注入組件選項(xiàng)
Vue.mixin({
created: function () {
// 邏輯...
}
...
})
// 4. 添加實(shí)例方法
Vue.prototype.$myMethod = function (methodOptions) {
// 邏輯...
}
}
數(shù)字驗(yàn)證碼:
import SIdentify from './src/identify'
/* istanbul ignore next */
SIdentify.install = function (Vue) {
Vue.component(SIdentify.name, SIdentify)
}
export default SIdentify
<template>
<div class="s-canvas" style="display: flex;">
<canvas id="s-canvas" :width="contentWidth" :height="contentHeight" @click="getValidCode"></canvas>
</div>
</template>
<script>
export default {
name: 'SIdentify',
props: {
identifyCode: {
type: String,
default: '1234'
},
fontSizeMin: {
type: Number,
default: 16
},
fontSizeMax: {
type: Number,
default: 40
},
backgroundColorMin: {
type: Number,
default: 180
},
backgroundColorMax: {
type: Number,
default: 240
},
colorMin: {
type: Number,
default: 50
},
colorMax: {
type: Number,
default: 160
},
lineColorMin: {
type: Number,
default: 40
},
lineColorMax: {
type: Number,
default: 180
},
dotColorMin: {
type: Number,
default: 0
},
dotColorMax: {
type: Number,
default: 255
},
contentWidth: {
type: Number,
default: 153
},
contentHeight: {
type: Number,
default: 40
}
},
methods: {
// 生成一個(gè)隨機(jī)數(shù)
randomNum (min, max) {
return Math.floor(Math.random() * (max - min) + min)
},
// 生成一個(gè)隨機(jī)的顏色
randomColor (min, max) {
const r = this.randomNum(min, max)
const g = this.randomNum(min, max)
const b = this.randomNum(min, max)
return 'rgb(' + r + ',' + g + ',' + b + ')'
},
drawPic () {
const canvas = document.getElementById('s-canvas')
const ctx = canvas.getContext('2d')
ctx.textBaseline = 'bottom'
// 繪制背景
ctx.fillStyle = this.randomColor(this.backgroundColorMin, this.backgroundColorMax)
ctx.fillRect(0, 0, this.contentWidth, this.contentHeight)
// 繪制文字
for (let i = 0; i < this.identifyCode.length; i++) {
this.drawText(ctx, this.identifyCode[i], i)
}
this.drawLine(ctx)
this.drawDot(ctx)
},
drawText (ctx, txt, i) {
ctx.fillStyle = this.randomColor(this.colorMin, this.colorMax)
ctx.font = this.randomNum(this.fontSizeMin, this.fontSizeMax) + 'px SimHei'
const x = (i + 1) * (this.contentWidth / (this.identifyCode.length + 1))
const y = this.randomNum(this.fontSizeMax, this.contentHeight - 5)
const deg = this.randomNum(-45, 45)
// 修改坐標(biāo)原點(diǎn)和旋轉(zhuǎn)角度
ctx.translate(x, y)
ctx.rotate(deg * Math.PI / 180)
ctx.fillText(txt, 0, 0)
// 恢復(fù)坐標(biāo)原點(diǎn)和旋轉(zhuǎn)角度
ctx.rotate(-deg * Math.PI / 180)
ctx.translate(-x, -y)
},
drawLine (ctx) {
// 繪制干擾線
for (let i = 0; i < 8; i++) {
ctx.strokeStyle = this.randomColor(this.lineColorMin, this.lineColorMax)
ctx.beginPath()
ctx.moveTo(this.randomNum(0, this.contentWidth), this.randomNum(0, this.contentHeight))
ctx.lineTo(this.randomNum(0, this.contentWidth), this.randomNum(0, this.contentHeight))
ctx.stroke()
}
},
drawDot (ctx) {
// 繪制干擾點(diǎn)
for (let i = 0; i < 100; i++) {
ctx.fillStyle = this.randomColor(0, 255)
ctx.beginPath()
ctx.arc(this.randomNum(0, this.contentWidth), this.randomNum(0, this.contentHeight), 1, 0, 2 * Math.PI)
ctx.fill()
}
},
getValidCode () {
this.$emit('getValidCode')
}
},
watch: {
identifyCode () {
this.drawPic()
}
},
mounted () {
this.drawPic()
}
}
</script>
提示信息
import OperMsg from './components/OperMsg.vue'
const OperTip = {
install: function (Vue, options = {}) {
const VueMsg = Vue.extend(OperMsg)
let msg = null
const $operTip = {
show(methodOps = {}) {
return new Promise(resolve => {
let defaultNote = '操作成功'
if (!msg) {
msg = new VueMsg()
console.log(msg.$props)
if (msg.$props.msgType === 'error') {
defaultNote = '操作失敗'
}
msg.$props.msgType = methodOps.type || 'success'
msg.$props.msgContent = methodOps.content || defaultNote
msg.$mount()
document.querySelector(options.container || 'body').appendChild(msg.$el)
}
if (msg.$props.msgType === 'error') {
defaultNote = '操作失敗'
}
msg.$props.msgType = methodOps.type || 'success'
msg.$props.msgContent = methodOps.content || defaultNote
msg.show()
resolve()
})
},
hide() {
return new Promise(resolve => {
if (!msg) {
resolve()
return
}
msg.hide()
})
}
}
Vue.operTip = Vue.prototype.$operTip = $operTip
// 注冊(cè)組件
Vue.component('operTip', OperMsg)
}
}
export default OperTip
<template>
<div class="oper-msg-container"
ref="msgContainer">
<transition name="slide">
<div class="msg"
v-if="isShowMsg === 'show'"
:style='{ backgroundColor: color }'>
<img src="../../../assets/personalCenter/oper-tip-icon.png">
{{ msgContent.length > 15 ? msgContent.substr(0, 15) + '...' : msgContent }}
</div>
</transition>
</div>
</template>
<script>
export default {
name: 'OperMsg',
props: {
msgType: {
type: String,
default: 'success'
},
msgContent: {
type: String,
default: ''
}
},
data () {
return {
isShowMsg: 'hide',
timer: null,
clearHeightTimer: null,
color: '#FFA300'
}
},
mounted () {
},
watch: {
isShowMsg (oldVal, newVal) {
if (this.timer) {
clearTimeout(this.timer)
}
if (newVal === 'hide') {
this.timer = setTimeout(() => {
this.hide()
}, 3000)
}
}
},
methods: {
show () {
console.log(this.msgType)
if (this.msgType === 'error') {
this.color = '#ED6355'
} else {
this.color = '#FFA300'
}
this.isShowMsg = 'show'
},
hide () {
this.isShowMsg = 'hide'
}
}
}
</script>
<style lang="scss" scoped>
.oper-msg-container {
width: 100%;
display: flex;
justify-content: center;
align-items: center;
z-index: 10000;
position: fixed;
top: 5px;
top: calc(5px + constant(safe-area-inset-top));
top: calc(5px + env(safe-area-inset-top));
background: transparent;
.msg {
display: flex;
justify-content: center;
align-items: center;
padding: 10px 30px;
font-size: 14px;
font-weight: 500;
color: rgba(255, 255, 255, 1);
line-height: 14px;
// background: rgba(255,163,0,1);
border-radius: 20px;
img {
width: 20px;
height: 20px;
}
}
.slide-enter-active {
animation: slideInDown 0.5s;
}
.slide-leave-active {
animation: slideOutUp 0.5s;
}
@-webkit-keyframes slideInDown {
from {
-webkit-transform: translate3d(0, -100%, 0);
transform: translate3d(0, -100%, 0);
visibility: visible;
}
to {
-webkit-transform: translate3d(0, 0, 0);
transform: translate3d(0, 0, 0);
}
}
@-webkit-keyframes slideOutUp {
from {
-webkit-transform: translate3d(0, 0, 0);
transform: translate3d(0, 0, 0);
}
to {
visibility: hidden;
-webkit-transform: translate3d(0, -100%, 0);
transform: translate3d(0, -100%, 0);
}
}
}
</style>