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>
相关推荐
林恒smileZAZ39 分钟前
Vue<前端页面版本检测>
前端·javascript·vue.js
ZhengEnCi2 小时前
M3-markconv库找不到wkhtmltopdf问题
python
码事漫谈4 小时前
当AI开始“思考”:我们是否真的准备好了?
前端·后端
2301_764441334 小时前
LISA时空跃迁分析,地理时空分析
数据结构·python·算法
许杰小刀4 小时前
ctfshow-web文件包含(web78-web86)
android·前端·android studio
我是Superman丶5 小时前
Element UI 表格某行突出悬浮效果
前端·javascript·vue.js
恋猫de小郭5 小时前
你的代理归我了:AI 大模型恶意中间人攻击,钱包都被转走了
前端·人工智能·ai编程
chushiyunen5 小时前
python rest请求、requests
开发语言·python
cTz6FE7gA5 小时前
Python异步编程:从协程到Asyncio的底层揭秘
python
xiaokuangren_5 小时前
前端css颜色
前端·css