中文 | English
Click interception with a single directive. Register a global check function — every click goes through it before reaching your handler.
npm install vue-intercept-pluginimport { createApp } from 'vue'
import VueInterceptPlugin from 'vue-intercept-plugin'
const app = createApp(App)
app.use(VueInterceptPlugin, {
check: (ctx) => {
// Return true to allow, false to deny
return userStore.hasPermission('delete')
},
onDeny: (ctx) => {
// Optional: called when denied
ElMessage.warning('No permission')
},
})import Vue from 'vue'
import VueInterceptPlugin from 'vue-intercept-plugin'
Vue.use(VueInterceptPlugin, {
check: (ctx) => userStore.hasPermission('delete'),
onDeny: (ctx) => ElMessage.warning('No permission'),
})Two forms:
<!-- Pass a function directly -->
<button v-intercept="handleDelete">Delete</button>
<el-button v-intercept="handleDelete">Delete</el-button>
<!-- Array format: first is function, rest are args -->
<button v-intercept="[handleDeleteById, 1001]">Delete Order #1001</button>
<el-button v-intercept="[handleDeleteById, 1001, 'abc', true]">Batch</el-button>const handleDelete = () => { deleteApi() }
const handleDeleteById = (id: number, str: string, flag: boolean) => {
deleteApi(id, str, flag)
}check and onDeny receive:
interface InterceptContext {
el: HTMLElement
handler: Function
value: unknown
}| Template | ctx.value |
Actual call |
|---|---|---|
v-intercept="fn" |
fn |
fn() |
v-intercept="[fn]" |
[fn] |
fn() |
v-intercept="[fn, 100]" |
[fn, 100] |
fn(100) |
v-intercept="[fn, 100, 'a']" |
[fn, 100, 'a'] |
fn(100, 'a') |
MIT