Issue
There is an interesting example on the Vue.js site that reverses a string using JS running in the browser. How can I recreate this example by passing the string to Python using Flask, reversing the string in Python, and then sending the result back to Vue - instead of doing the string reversal in the browser?
HTML
<div id="app-5">
<p>{{ message }}</p>
<button v-on:click="reverseMessage">Reverse Message</button>
</div>
JS
var app5 = new Vue({
el: '#app-5',
data: {
message: 'Hello Vue.js!'
},
methods: {
reverseMessage: function () {
this.message = this.message.split('').reverse().join('')
}
}
})
Source: https://v2.vuejs.org/v2/guide/
Solution
you can use fetch
api or some http lib that vue uses like vue-resource but it doesn't matter.
code could look something like this
methods: {
reverseMessage: function() {
fetch('/route', {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify(this.message)
})
.then(response => response.json())
.then(data => this.message = data)
}
}
Answered By - dankobgd
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.