当前位置: 首页 > news >正文

python EEL + vue3.js 项目中如何把组件中的函数提升为全局函数

eel官方示例中暴露的js函数是全局函数,vue中的自定义函数作用域通常都是组件范围内。要让eel.js调用,需要将其升为全局可用。

一般方法有 app.config.globalProperties  或 mixin等。

main.js

//main.jsimport { createApp } from 'vue'
import App from './App.vue'const app = createApp(App);// 在 Vue 3 中添加全局属性(例如 `$eel`)
app.config.globalProperties.$eel = window.eel;app.mount('#app');

 App.vue

<template><img alt="Vue logo" src="./assets/vue.svg"> python eel + Vue3<HelloWorld />
</template><script>import HelloWorld from './components/HelloWorld.vue';export default {name: 'App',components: {HelloWorld}
}</script><style>
#app {text-align: center;margin-top: 60px;
}
</style>

HelloWorld.vue 

// HelloWorld.vue<script setup>//定义getCurrentInstance().appContext.config.globalProperties
import { ref, onMounted,getCurrentInstance } from 'vue'
const instance = getCurrentInstance();
const globalProperties = instance.appContext.config.globalProperties;// create a reactive count using ref
const count = ref(0)
const message = ref("Hello World!")const sayHelloByEEL = (x) => {globalProperties.$eel.say_hello_py(x + Math.floor(Math.random() * 101).toString())
}const sayHelloJS = (x) => {message.value = 'Say Hello From ' + Math.floor(Math.random() * 101).toString() +x;
}//使用 globalProperties 暴露sayHelloJS给Python (以say_hello_js为别名)
globalProperties.$eel.expose(sayHelloJS, 'say_hello_js')onMounted(() => {console.log(globalProperties)
})</script><template><p><button type="button" @click="count++">count is: {{ count }}</button></p><p><button @click="sayHelloByEEL('Javascript Button! ')">调用Python函数</button></p><p>{{ message }}</p><p><button @click="sayHelloJS(' JS Button!')">调用JS函数</button></p></template><style scoped></style>

http://www.lryc.cn/news/350074.html

相关文章:

  • sqli-labs靶场第十四关
  • 【C语言】6.C语言VS实用调试技巧(1)
  • AIGC行业现在适合进入吗
  • ubuntu CUDA 驱动更新,版本更新,多CUDA版本管理
  • effective python学习笔记_类与接口
  • 如何去除字符串两侧的空白字符?
  • Flutter 中的 PageStorage 小部件:全面指南
  • 头歌实践教学平台:CG1-v2.0-直线绘制
  • Nacos+GateWay 搭建微服务架构
  • 【2024华为HCIP831 | 高级网络工程师之路】刷题日记(18)
  • 在抖音做电商,没有货源,不懂直播怎么办?分享一种解决方案!
  • 基于单片机的智能安防系统设计(32+4G+WIFI版)-设计说明书
  • 云服务器配置mysql允许被远程连接从而使用图形化界面
  • 【软件测试】需求概念|软件的⽣命周期|开发模型|测试模型
  • SQL中的LAG函数与LEAD函数用法
  • 数据结构------二叉树经典习题1
  • 汇聚荣:拼多多长期没有流量如何提高?
  • Chrome的常用操作总结
  • dvwa靶场 JavaScript Attacks(js攻击)全难度教程(附代码分析)
  • Flutter 中的 checkboxListTile 小部件:全面指南
  • 前馈神经网络FNN、多层感知机MLP和反向传播推导
  • QML笔记八
  • 运维别卷系列 - 云原生监控平台 之 00.prometheus 监控汇总
  • 信息系统安全与对抗-网络侦查技术与网络扫描技术(期末复习简答题)
  • 【python量化交易】—— Alpha选股策略 - Qteasy自定义交易策略【附源码】
  • 简单记录下:Navicat 导出表结构至 Excel
  • 黑马基于Web-socket的java聊天室基本解析
  • 【操作系统期末速成】​内存管理|内存的装入模块在装入内存的方式|分配管理方式|页面置换算法|页面置换
  • 图和网络笔记
  • 请求外部系统报错