Vue3与pywebview前后端初步通信

1、pywebview后端

python 复制代码
class Api:
    def greet(self, test_text):
        print(test_text)
        return f"hello, {test_text}"


if __name__ == '__main__':
    # 前后端通信测试
    api = Api()
    window = webview.create_window('Vue app in pywebview', './static/index.html', js_api=api)   # vue的build文件的路径
    webview.start(debug=True)

2、Vue3前端

javascript 复制代码
<template>
  <div id="app">
    <h1>Greeting Test</h1>
    <button @click="greet">Greet</button>
    <p>{{ greeting }}</p>
  </div>
</template>

<script>
export default {
  data() {
    return {
      greeting: ''
    };
  },
  methods: {
    greet() {
      // 调用后端API
      if (window.pywebview) {
        window.pywebview.api.greet('Socket test').then(response => {
          this.greeting = response;
          console.log(this.greeting);
        });
      }
    }
  }
};
</script>

<style>
#app {
  text-align: center;
  margin-top: 50px;
}
</style>
相关推荐
吴佳浩1 天前
Python入门指南(五) - 为什么选择 FastAPI?
后端·python·fastapi
寰天柚子1 天前
Java并发编程中的线程安全问题与解决方案全解析
java·开发语言·python
晚烛1 天前
实战前瞻:构建高可靠、强协同的 Flutter + OpenHarmony 智慧教育平台
javascript·flutter·html
2503_928411561 天前
项目中的一些问题(补充)
人工智能·python·tensorflow
superman超哥1 天前
仓颉语言中锁的实现机制深度剖析与并发实践
c语言·开发语言·c++·python·仓颉
vv_Ⅸ1 天前
打卡day42
python
快乐肚皮1 天前
一文了解XSS攻击:分类、原理与全方位防御方案
java·前端·xss
保护我方头发丶1 天前
ESP-wifi-蓝牙
前端·javascript·数据库
想学后端的前端工程师1 天前
【Flutter跨平台开发实战指南:从零到上线-web技术栈】
前端·flutter
Lvan的前端笔记1 天前
python:深入理解 Python 的 `__name__ == “__main__“` 与双下划线(dunder)机制
开发语言·python