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>
相关推荐
IT_陈寒几秒前
SpringBoot 3.2实战:我用这5个冷门特性将接口QPS提升了200%
前端·人工智能·后端
2501_944441752 分钟前
Flutter&OpenHarmony商城App消息通知组件开发
java·javascript·flutter
小流苏生6 分钟前
当你不再热爱自己的工作和生活……
前端·程序员·ai编程
Aliex_git6 分钟前
Vue 2 - 模板编译源码理解
前端·javascript·vue.js·笔记·前端框架
Irene19918 分钟前
Vue:Props 和 Emits 对比总结
vue.js·props·emits
saadiya~8 分钟前
实战笔记:在 Ubuntu 离线部署 Vue + Nginx 踩坑与避雷指南
vue.js·笔记·nginx
Thetimezipsby9 分钟前
Redux、React Redux 快速学习上手、简单易懂、知识速查
前端·react.js·redux
郝学胜-神的一滴11 分钟前
Python面向对象编程:解耦、多态与魔法艺术
java·开发语言·c++·python·设计模式·软件工程
黎明初时13 分钟前
react基础框架搭建2-准备工作:react+router+redux+axios+Tailwind+webpack
前端·react.js·webpack
AI云原生16 分钟前
如何解决 pip install 代理报错 SOCKS5 握手失败 ReadTimeoutError 问题
网络·爬虫·python·网络协议·tcp/ip·scikit-learn·pip