How to turn v-on:click="Sync" into an automatic trigger when opening html

Asked

Viewed 260 times

1

Explanation of the problem

Have this javascript in html I call v-on:click="sync" it updates the news list, but this news list until I click Sync it goes blank, so I’m looking for a way to make the v-on:click="sync" on something automatic that when I open HTML the news list is automatically updated.

The Javascript to follow

document.addEventListener("deviceready",onDeviceReady,false);
function onDeviceReady(){
var vm = new Vue({
        el: '#app',
        data:{
            noticias: []
        },
        methods:{
            sync:function(){
                $.ajax({
                    dataType: 'json',
                    url: 'http://example/read.php',
                    success:function(dados){
                        localStorage.setItem('noticias',JSON.stringify(dados));
                        vm.setNoticias();
                    },
                    error:function(){
                        alert("ocorreu um erro durante a conexão com o servidor!");
                    }
            });
        },
        setNoticias:function(){
            this.noticias = JSON.parse(localStorage.getItem('noticias'));
            console.log(this.noticias);
        }
    },
    ready:function(){
     this.setNoticias();
        }
    })
}
  • I suggest editing the title. It can be more descriptive than it is currently.

  • What version of Vue are you using? 1 or 2?

  • I’m using version 1

1 answer

1


You can take the creation of the function vm and right after calling the function:

var vm = new Vue({...});

vm.setNoticias();
  • Could you be more specific? I don’t quite understand what you meant

  • You take your creation function var vm = new Vue({...}), put in a separate script, just after the Vue. At the end of your script you call the function vm.setNoticias();

  • I could post a clearer example?

  • @Yuriruan your script will stay thus and you must put the tag script for him only after importing the library vue

Browser other questions tagged

You are not signed in. Login or sign up in order to post.