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>
相关推荐
Wect11 分钟前
LeetCode 39. 组合总和:DFS回溯解法详解
前端·算法·typescript
Wect14 分钟前
LeetCode 46. 全排列:深度解析+代码拆解
前端·算法·typescript
IT_陈寒15 分钟前
Vite 凭什么比 Webpack 快50%?揭秘闪电构建背后的黑科技
前端·人工智能·后端
颜酱16 分钟前
Dijkstra 算法:从 BFS 到带权最短路径
javascript·后端·算法
hi大雄1 小时前
我的 2025 —— 名为《开始的勇气》🌱
前端·年终总结
从文处安1 小时前
「前端何去何从」一直写 Vue ,为何要在 AI 时代去学 React?
前端·react.js
aircrushin1 小时前
OpenClaw“养龙虾”现象的社会技术学分析
前端·后端
明君879971 小时前
#Flutter 的官方Skills技能库
前端·flutter
yuki_uix1 小时前
重新认识 React Hooks:从会用到理解设计
前端·react.js
林太白2 小时前
ref和reactive对比终于学会了
前端