Warning: error_log(/data/www/wwwroot/hmttv.cn/caches/error_log.php): failed to open stream: Permission denied in /data/www/wwwroot/hmttv.cn/phpcms/libs/functions/global.func.php on line 537 Warning: error_log(/data/www/wwwroot/hmttv.cn/caches/error_log.php): failed to open stream: Permission denied in /data/www/wwwroot/hmttv.cn/phpcms/libs/functions/global.func.php on line 537
ue-contextmenu 右鍵彈出菜單插件,新增加了無限子菜單功能。
// main.js
import Vue from 'vue';
import ContextMenu from '@/plugins/ContextMenu';
Vue.use(ContextMenu);
<script>
import { ContextMenu, ContextMenuDirective } from '@/plugins/ContextMenu';
export default {
directives: {
// 引入指令式 指令式寫法見下文
'v-contextmenu': ContextMenuDirective
},
methods: {
openMenu() {
// 引入函數式
ContextMenu({
event, // 傳入鼠標事件
menu: [
{ icon: 'el-icon-view', name: this.$t('button.view'), action: 'view' },
{ icon: 'el-icon-edit', name: this.$t('button.edit'), action: 'edit' },
{ icon: 'el-icon-delete', name: this.$t('button.delete'), action: 'delete',
children: [
{
icon: 'el-icon-price-tag',
name: '新增1',
action: 'newMemoryTag',
},
{
icon: 'el-icon-price-tag',
name: '新增2',
action: 'newExpressionTag',
}
]
}
]
}).then(rs=> {
switch (rs) {
case 'view':
this.viewFn();
break;
case 'edit':
this.editFn();
break;
case 'delete':
this.deleteFn();
break;
}
});
}
}
};
</script>
<template>
<div class="panel__wrap" v-contextmenu="menu">
some inner html or other ...
</div>
</template>
<script>
// 以下兩種場景均以全局引入方式引入
export default {
methods: {
viewFn(event) {},
editFn(event) {},
deleteFn(event) {}
},
computed: {
menu () {
return [{
icon: 'el-icon-view',
name: this.$t('button.view'),
fn: this.viewFn
}, {
icon: 'el-icon-edit',
name: this.$t('button.edit'),
fn: this.editFn
}, {
icon: 'el-icon-delete',
name: this.$t('button.delete'),
fn: this.deleteFn
}]
}
}
};
</script>
<template>
<el-table @row-contextmenu="rowContextmenu">
some inner html or other ...
</el-table>
</template>
<script>
export default {
data() {
return {
tableData: []
};
},
methods: {
rowContextmenu(row, event) {
this.$ContextMenu({
event, // 傳入鼠標事件
menu: [
{ icon: 'el-icon-view', name: this.$t('button.view'), action: 'view' },
{ icon: 'el-icon-edit', name: this.$t('button.edit'), action: 'edit' },
{ icon: 'el-icon-delete', name: this.$t('button.delete'), action: 'delete' }
]
}).then(rs=> {
switch (rs) {
case 'view':
this.viewFn();
break;
case 'edit':
this.editFn();
break;
case 'delete':
this.deleteFn();
break;
}
});
}
}
};
</script>
<template>
<el-table @row-contextmenu="rowContextmenu">
some inner html or other ...
</el-table>
</template>
<script>
export default {
data() {
return {
tableData: []
};
},
methods: {
rowContextmenu(row, event) {
this.$ContextMenu({
event, // 傳入鼠標事件
menu: [
{ icon: 'el-icon-view', name: this.$t('button.view'), fn: this.viewFn },
{ icon: 'el-icon-edit', name: this.$t('button.edit'), fn: this.editFn },
{ icon: 'el-icon-delete', name: this.$t('button.delete'), fn: this.deleteFn }
]
});
},
viewFn(event) {},
editFn(event) {},
deleteFn(event) {}
}
};
</script>
最近接到一個需求,需要在一些敏感操作進行前要求輸入賬號和密碼,然后將輸入的賬號和密碼加到接口請求的header里面。如果每個頁面都去手動導入彈窗組件,在點擊按鈕后彈出彈窗。再拿到彈窗返回的賬號密碼后去請求接口也太累了,那么有沒有更簡單的實現方式呢?
首先我們來看看什么是函數式彈窗?
函數式彈窗是一種使用函數來創建彈窗的技術。它可以簡化彈窗的使用,只需要在需要彈窗的地方調用函數就可以了。那么這里使用函數式彈窗就能完美的解決我們的問題。
我們只需要封裝一個showPasswordDialog函數,調用該函數后會彈出一個彈窗。該函數會返回一個resolve后的值就是賬號密碼的Promise。然后在http請求攔截器中加一個needValidatePassword字段,攔截請求時如果該字段為true,就await調用showPasswordDialog函數。拿到賬號和密碼后塞到請求的header里面。這樣就我們就只需要在發起請求的地方加一個needValidatePassword: true配置就行了。
這個是簡化后template中的代碼,和Element Plus官網中的demo代碼差不多,沒有什么說的。
<template>
<el-dialog :model-value="visible" title="賬號和密碼" @close="handleClose">
<!-- 省略賬號、密碼表單部分... -->
<el-button type="primary" @click="submitForm()">提交</el-button>
</el-dialog>
</template>
這個是簡化后的script代碼,大部分和Element Plus官網的demo代碼差不多。需要注意的是我們這里將close關閉事件和confirm確認事件定義在了props中,而不是在emits中,因為后面函數式組件會通過props將這兩個回調傳入進來。具體的我們下面會講。
<script setup lang="ts">
interface Props {
visible: boolean;
close?: ()=> void;
confirm?: (data)=> void;
}
const props=defineProps<Props>();
const emit=defineEmits(["update:visible"]);
const submitForm=async ()=> {
// 省略validate表單校驗的代碼
// 這里的data為表單中輸入的賬號密碼
props.confirm?.(data);
handleClose();
};
const handleClose=()=> {
emit("update:visible", false);
props.close?.();
};
</script>
createApp函數會創建和返回一個vue的應用實例,也就是我們平時常說的app,該函數接受兩個參數。第一個參數為接收一個組件,也就是我們平時寫的vue文件。第二個參數為可選的對象,這個對象會傳遞給第一個參數組件的props。
舉個例子:
import MyComponent from "./MyComponent"
const app=createApp(MyComponent, {
visible: true
})
在這個例子中我們基于MyComponent組件生成了一個app應用實例,如果MyComponent組件的props中有定義visible,那么visible就會被賦值為true。
調用createApp函數創建的這個應用實例app實際就是在內存中創建的一個對象,并沒有渲染到瀏覽器的dom上面。這個時候我們就要調用應用實例app暴露出來的mount方法將這個組件掛載到真實的dom上面去。mount方法接收一個“容器”參數,用于將組件掛載上去,可以是一個實際的 DOM 元素或是一個 CSS 選擇器字符串。比如下面這個例子是將組件掛載到body上面:
app.mount(document.body)
app提供了很多方法和屬性,詳見 vue官網。
我們希望showPasswordDialog函數返回一個Promise,resolve的值就是彈窗中輸入的表單。例如,我們可以使用以下代碼使用showPasswordDialog函數:
try {
// 調用這個就會彈出彈窗
const res: RuleForm=await showPasswordDialog();
// 這個res就是輸入的賬號密碼
console.log("res", res);
} catch (error) {
console.log(error);
}
經過上面的介紹我們知道了可以調用createApp函數傳入指定組件生成app,然后使用app.mount方法將這個組件掛載到指定的dom上面去。那么現在思路就清晰了,我們只需要將我們前面實現的彈窗組件作為第一個參數傳遞給createApp函數。第二個參數傳入一個對象給彈窗組件的props,用以控制打開彈窗和注冊彈窗關閉和確認的事件回調。下面是實現的showPasswordDialog函數
import { App, createApp } from "vue";
import PasswordDialog from "./index.vue";
// 這個index.vue就是我們前面實現的彈窗組件
export async function showPasswordDialog(): Promise<RuleForm> {
return new Promise((resolve, reject)=> {
let mountNode=document.createElement("div");
let dialogApp: App<Element> | undefined=createApp(PasswordDialog, {
visible: true,
close: ()=> {
if (dialogApp) {
dialogApp.unmount();
document.body.removeChild(mountNode);
dialogApp=undefined;
reject("close");
}
},
confirm: (res: RuleForm)=> {
resolve(res);
dialogApp?.unmount();
document.body.removeChild(mountNode);
dialogApp=undefined;
},
});
document.body.appendChild(mountNode);
dialogApp.mount(mountNode);
});
}
在這個showPasswordDialog函數中我們先創建了一個div元素,再將彈窗組件傳遞給了createApp函數生成一個dialogApp的實例。然后將創建的div元素掛載到body上面,再調用mount方法將我們的彈窗組件掛載到創建的div元素上,至此我們實現了通過函數式調用將彈窗組件渲染到body中。
現在我們再來看看傳入到createApp函數的第二個對象參數,我們給這個對象分別傳入了visible屬性、close和confirm回調方法,分別會賦值給彈窗組件props中的visible、close、confirm。
彈窗組件中觸發關閉事件時會調用props.close?.(),實際這里就是在調用我們傳入的close回調方法。在這個方法中我們調用了實例的unmount方法卸載組件,然后將創建的彈窗組件dom從body中移除,并且返回一個reject的Promise。
當我們將賬號和密碼輸入完成后,會調用props.confirm?.(ruleForm),這里的ruleForm就是我們表單中的賬號和密碼。實際這里就是在調用我們傳入的confirm回調方法,接下來同樣也是卸載組件和移除彈窗組件生成的dom,并且返回一個resolve值為賬號密碼表單的Promise。
這篇文章主要介紹了如何創建函數式彈窗:
作者:歐陽碼農
鏈接:https://juejin.cn/post/7322229620391673908
lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>demo</title>
<style>
body,h1,h2,h3,h4,h5,h6,hr,p,blockquote,dl,dt,dd,ul,ol,li,pre,form,fieldset,legend,button,input,textarea,th,td{margin:0;padding:0;}
body,button,input,select,textarea{font:12px/1.5 tahoma,arial,b8bf53;}
h1,h2,h3,h4,h5,h6{font-size:100%;}
address,cite,dfn,em,var{font-style:normal;}
code,kbd,pre,samp{font-family:courier new,courier,monospace;}
small{font-size:12px;}
ul,ol{list-style:none;}
a{text-decoration:none;}
a:hover{text-decoration:underline;}
sup{vertical-align:text-top;}
sub{vertical-align:text-bottom;}
legend{color:#000;}
fieldset,img{border:0;}
button,input,select,textarea{font-size:100%;}
table{border-collapse:collapse;border-spacing:0;}
.clear{clear: both;float: none;height: 0;overflow: hidden;}
p{font-size: 100px;}
#mask{
background: #000;
opacity: 0.75;
filter: alpha(opacity=75);
height: 1000px;
width: 100%;
position: absolute;
left: 0;
top: 0;
z-index: 1000;
}
#login{
position: fixed;
left: 30%;
top: 30%;
z-index:1001;
}
.loginCon{
width: 670px;
height: 380px;
background: #fff;
border:5px solid #F01400;
}
#close{
width: 30px;
height: 30px;
background: blue;
cursor: pointer;
position: absolute;
right: 10px;
top: 10px;
}
#btnLogin{
width: 80px;
height: 40px;
background: #F01400;
margin:0 auto;
display: block;
cursor: pointer;
border-style: none;
color: #fff;
font-size: 16px;
}
</style>
</head>
<body>
<button id="btnLogin">登錄</button>
<!--
<div id="mask"></div>
<div id="login">
<div class="loginCon">
<div id="close"></div>
</div>
</div>
-->
<script type="text/javascript">
function openNew(){
//獲取頁面body!內容!的高度和寬度
var sHeight=document.documentElement.scrollHeight;
var sWidth=document.documentElement.scrollWidth;
//獲取可視區域高度,寬度與頁面內容的寬度一樣
var wHeight=document.documentElement.clientHeight;
//創建遮罩層div并插入body
var oMask=document.createElement("div");
oMask.id="mask";
oMask.style.height=sHeight+"px";
//寬度直接用100%在樣式里
document.body.appendChild(oMask);
//創建登錄層div并插入body
var oLogin=document.createElement("div");
oLogin.id="login";
oLogin.innerHTML="<div class='loginCon'><div id='close'></div></div>"
document.body.appendChild(oLogin);
//獲取login的寬度和高度并設置偏移值
var dHeight=oLogin.offsetHeight;
var dWidth=oLogin.offsetWidth;
oLogin.style.left=(sWidth-dWidth)/2+"px";
oLogin.style.top=(wHeight-dHeight)/2+"px";
//獲取關閉按鈕
var oClose=document.getElementById("close");
oMask.onclick=oClose.onclick=function(){
document.body.removeChild(oMask);
document.body.removeChild(oLogin);
}
}
window.onload=function(){
var oBtn=document.getElementById("btnLogin");
oBtn.onclick=function(){
openNew();
}
}
</script>
</body>
</html>
*請認真填寫需求信息,我們會在24小時內與您取得聯系。