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>
相关推荐
摘星编程几秒前
React Native鸿蒙版:React Query无限滚动
javascript·react native·react.js
【赫兹威客】浩哥3 分钟前
【赫兹威客】框架模板-后端bat脚本部署教程
python·django
Cestb0n4 分钟前
某果app 加密校验算法逆向分析
python·算法·逆向安全
web守墓人5 分钟前
【前端】vue3的指令
前端
薛定谔的猫喵喵7 分钟前
解决 xlrd 2.0+ 版本只支持 xls 格式的问题
python·excel
2501_9418053121 分钟前
使用Python和Go构建高性能分布式任务调度系统的实践分享
分布式·python·golang
OLOLOadsd12336 分钟前
基于YOLO11-C3k2-LFE的岩石颗粒智能检测与识别系统_2
python
_pass_41 分钟前
Agent-Chat模式核心流程解析
python
想起你的日子44 分钟前
EFCore之Code First
前端·.netcore
徐111 小时前
自动化检测系统投资回报分析:思看科技如何助力企业降本增效
python·科技·物联网·自动化