1. 使用生命周期钩子函数
class Api:
def greet(self):
greet_text= 'pywebview and vue3'
response = {}
response['text'] = greet_text
return response
if __name__ == '__main__':
api = Api()
window = webview.create_window('Vue app in pywebview', './static/index.html', js_api=api)
webview.start(debug=True)
<template>
<div>{{ data }}</div>
</template>
<script>
export default {
data() {
return {
data: null
};
},
created() {
this.fetchData();
},
methods: {
fetchData() {
window.pywebview.api.greet().then(response=> {
this.data= response['text'];
});
}
}
};
</script>
2、使用定时器
<template>
<div>{{ data }}</div>
</template>
<script>
export default {
data() {
return {
data: null,
timer: null
};
},
created() {
this.fetchData();
this.timer = setInterval(this.fetchData, 5000);
},
methods: {
fetchData() {
window.pywebview.api.greet().then(response=> {
this.data= response['text'];
});
},
cancelAutoUpdate() {
clearInterval(this.timer);
}
},
beforeDestroy() {
this.cancelAutoUpdate();
}
};
</script>