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>
相关推荐
爱吃奶酪的松鼠丶6 小时前
React长列表,性能优化。关于循环遍历的时候,key是用对象数据中的ID还是用索引
javascript·react.js·性能优化
Data_agent6 小时前
Python 编程实战:函数与模块化编程及内置模块探索
开发语言·python
十铭忘6 小时前
windows系统python开源项目环境配置1
人工智能·python
Generalzy6 小时前
langchain deepagent框架
人工智能·python·langchain
xkxnq6 小时前
第二阶段:Vue 组件化开发(第 17天)
javascript·vue.js·ecmascript
栈与堆7 小时前
LeetCode 19 - 删除链表的倒数第N个节点
java·开发语言·数据结构·python·算法·leetcode·链表
豆苗学前端7 小时前
你所不知道的前端知识,html篇(更新中)
前端·javascript·面试
一 乐7 小时前
绿色农产品销售|基于springboot + vue绿色农产品销售系统(源码+数据库+文档)
java·前端·数据库·vue.js·spring boot·后端·宠物
zzjyr7 小时前
Webpack 生命周期原理深度解析
前端
xiaohe06017 小时前
💘 霸道女总裁爱上前端开发的我
前端·游戏开发·trae