vue naive ui 按钮绑定按键

使用vue (naive ui) 绑定Enter 按键

知识点:

  • 按键绑定Button
  • 全局挂载使得message,notification, dialog, loadingBar 等NaiveUI 生效
  • UMD方式使用vue 与 naive ui
  • 将vue默认的 分隔符大括号 替换 为 [[ ]]
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>按钮绑定按键</title>
    <script src="https://unpkg.com/vue@3.3.4/dist/vue.global.js"></script>
    <script src="https://unpkg.com/naive-ui@2.34.4/dist/index.js"></script>
    <link href="http://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">

</head>
<body>


<div id="app">


    <n-button @click="compareClick" id="myButton">[[ button_name ]]</n-button>

</div>


<script>
    console.log("start")

    const disabledRef = Vue.ref(true);
    // message,notification, dialog, loadingBar 等生效的方法
    const {message, notification, dialog, loadingBar} = naive.createDiscreteApi(
        ["message", "dialog", "notification", "loadingBar"],
        {
            configProviderProps: naive.configProviderPropsRef
        }
    );


    const App = {
        setup() {

            document.onkeyup = function (e) {
                if (e.key == 'Enter') {
                    var myButton = document.getElementById("myButton");
                    message.info("您使用按键Enter触发了按钮,请稍后",
                        {
                            keepAliveOnHover: true
                        }
                    );
                    myButton.click()
                }
            }


            return {
                button_name: 'Button',

                compareClick() {
                    loadingBar.start();
                    disabledRef.value = false;


                    message.info("您已经点击了按钮,请稍后",
                        {
                            keepAliveOnHover: true
                        }
                    );
                    var timeInterval = 2000;
                    setTimeout(() => {
                        loadingBar.finish();

                        disabledRef.value = true;

                    }, timeInterval);


                }


            }
        }
    }

    // 将 默认的 分隔符大括号 替换 为 [[ ]]
    App.delimiters = ["[[", "]]"];
    const app = Vue.createApp(App)
    console.log("App.createApp ")
    app.use(naive)
    console.log("use naive")
    app.mount('#app')
    console.log("mount")
</script>
</body>
</html>

Enter 触发按钮

在这里插入图片描述

点击触发按钮

在这里插入图片描述

参考链接

  • https://juejin.cn/post/7188032240775856185
  • https://www.naiveui.com/zh-CN/os-theme/components/discrete
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值