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>
相关推荐
想吃火锅10054 小时前
【leetcode】405.数字转换为十六进制数js
开发语言·javascript·ecmascript
珺毅同学5 小时前
YOLO生成预测json标签迁移问题
python·yolo·json
骑士雄师5 小时前
18.4 长期记忆可修改版
python
~小先生~5 小时前
Python从入门到放弃(一)
开发语言·python
天佑木枫5 小时前
第2天:变量与数据类型 —— 让程序记住信息
python
原则猫6 小时前
HOOKS 背后机制
前端
码语智行6 小时前
首页导航跳转功能深度解析-系统内和系统外
前端
Dust-Chasing6 小时前
Claude Code源码剖析 - Claude Code 上下文压缩机制
人工智能·python·ai
阿猫的故乡7 小时前
Vue过渡动画从入门到装X:淡入淡出、滑动、列表动画、第三方库全搞定
前端·javascript·vue.js
裕波7 小时前
Vue&ViteConf 2026 将于 7 月 18 日在上海举办,尤雨溪将现场发表主题演讲
vue.js·vite