Pradyut
Pradyut

Reputation: 123

Update a variable in vue.js

I have a web app written in vue in which the user has to submit an amount. I want to create an integer variable total_amount that updates at each run. For eg, one user submits 50$, so total_amount should update to 50$, then another user submits 25$, then total_amount should update to 75$. So, I want a variable that updates it's value at the click of a button and then I'm returning this updated value to a function on the same page. Is there a way to achieve this on the frontend side itself, without any database or backend assistance?

data() {
    return {
      total_amount: 0
    }
}

I initialized total_amount in this way, but the problem is that it starts from 0 at each run, so it does not add on the previous amount rather just returns the current value.

Upvotes: 1

Views: 1161

Answers (1)

Zhyar Abdallah
Zhyar Abdallah

Reputation: 46

you need to use a backend server or a database to store that value , since this value gets accessed by deferent clients and session so it needs to be stored like a variable or a column in your database ,see below simple node Js backend

var http = require('http');

var total_amount= 0;

var server = http.createServer(function (req, res) {

    total_amount++;

    res.writeHead(200, { 'Content-Type': 'text/plain' });

    res.write('Hello!\n');

    res.write('We have had ' + total_amount+ ' visits!\n');

    res.end();

 

});

server.listen(9090);

console.log('server running...')

Upvotes: 1

Related Questions