Attribute not defined in a Vue object

2

I'm doing a few things in Vue. And I have a problem here. Apparently there is a mistake here and I have no idea what is wrong. The console says that title has not been set.

var cadastro = new Vue({
    el:'#cadastro',
    data:{
        pagina:0,
        titulo:["Dados Pessoais", "Endereço", "Curso"],
        titulo_atual: titulo[this.pagina]
    },
    methods:{
        passarPagina: function(e){
            e.preventDefault();
            this.pagina++;
        },

        voltarPagina: function(e){
            e.preventDefault();
            this.pagina--;
        }
    }
});
    
asked by anonymous 17.07.2017 / 23:41

1 answer

3

Here:

titulo_atual: titulo[this.pagina]

The this in this context is not the object of type Vue that is being created, but the this external (which depends on context, is probably window ). You can resolve this by using a computed property:

var cadastro = new Vue({
    el:'#cadastro',
    data:{
        pagina:0,
        titulo:["Dados Pessoais", "Endereço", "Curso"]
    },
    computed: {
        titulo_atual: function() {
            return this.titulo[this.pagina];
        }
    },
    methods:{
        passarPagina: function(e){
            e.preventDefault();
            this.pagina++;
        },

        voltarPagina: function(e){
            e.preventDefault();
            this.pagina--;
        }
    }
});
    
17.07.2017 / 23:55