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

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

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

main.js

//main.js


import { 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>

相关推荐

  1. React函数组件和类组件区别

    2024-05-16 04:46:08       28 阅读
  2. 变量和函数提升js问题)

    2024-05-16 04:46:08       68 阅读
  3. 变量和函数提升js问题)

    2024-05-16 04:46:08       39 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-05-16 04:46:08       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-05-16 04:46:08       101 阅读
  3. 在Django里面运行非项目文件

    2024-05-16 04:46:08       82 阅读
  4. Python语言-面向对象

    2024-05-16 04:46:08       91 阅读

热门阅读

  1. c语言之文件打开模式

    2024-05-16 04:46:08       36 阅读
  2. git 命令之 - revert

    2024-05-16 04:46:08       113 阅读
  3. Rust中的链式调用方法

    2024-05-16 04:46:08       110 阅读
  4. ANSYS Maxwell16 引导

    2024-05-16 04:46:08       28 阅读
  5. Django信号与扩展:深入理解与实践

    2024-05-16 04:46:08       33 阅读
  6. Golang面向对象编程

    2024-05-16 04:46:08       25 阅读
  7. 桥接模式举个例子简单理解

    2024-05-16 04:46:08       29 阅读
  8. js的跳转传参方式

    2024-05-16 04:46:08       31 阅读