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>
相关推荐
jiayu16 分钟前
Angular学习笔记24:Angular 响应式表单 FormArray 与 FormGroup 相互嵌套
前端
jiayu17 分钟前
Angular6学习笔记13:HTTP(3)
前端
小码哥_常20 分钟前
Kotlin抽象类与接口:相爱相杀的编程“CP”
前端
evelynlab21 分钟前
Tapable学习
前端
进击的尘埃23 分钟前
Vue3 响应式原理:从 Proxy 到依赖收集,手撸一个迷你 reactivity
javascript
willow35 分钟前
JavaScript数据类型整理1
javascript
LeeYaMaster35 分钟前
15个例子熟练异步框架 Zone.js
前端·angular.js
evelynlab38 分钟前
打包原理
前端
LeeYaMaster40 分钟前
20个例子掌握RxJS——第十一章实现 WebSocket 消息节流
javascript·angular.js
拳打南山敬老院1 小时前
Context 不是压缩出来的,而是设计出来的
前端·后端·aigc