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>
相关推荐
aiguangyuan1 分钟前
从零构建字符级RNN:用PyTorch实现莎士比亚风格文本生成
人工智能·python·nlp
Irene19916 分钟前
Vue 3 中的具名插槽仍然完全支持,Vue 2 的旧语法 Vue 3 中已废弃
vue.js·slot
方方洛14 分钟前
技术实践总结:schema-bridgion:json、xml、yaml、toml文件相互转换
xml·前端·typescript·node.js·json
梦幻精灵_cq17 分钟前
《双征color》诗解——梦幻精灵_cq对终端渲染的数据结构设计模型式拓展
数据结构·python
喵手34 分钟前
Python爬虫零基础入门【第八章:项目实战演练·第3节】上线与运维入门:定时运行、日志轮转、失败告警(轻量版)!
爬虫·python·爬虫实战·python爬虫工程化实战·零基础python爬虫教学·定时运行·日志轮转
2601_9495758638 分钟前
Flutter for OpenHarmony二手物品置换App实战 - 自定义组件实现
android·javascript·flutter
object not found40 分钟前
基于uniapp开发小程序自定义顶部导航栏状态栏标题栏
前端·javascript·小程序·uni-app
weixin_660096781 小时前
flash-attention总是安装失败
python·flash-attention
yaoxin5211231 小时前
303. Java Stream API - 查找元素
java·windows·python
Irene19911 小时前
v-model 在 Vue2 和 Vue3 中的实现对比或异同
vue.js