V
V
Vladislav2017-08-13 19:35:32
Vue.js
Vladislav, 2017-08-13 19:35:32

How to call a method in router-link?

Hello. How can a method be called inside a router-link? That is, something like this:

router-link(tag="div", :to="{path: '/user/'+ getUserId(result.id)}")

Well, respectively, in the method:
getUserId(uid) {
                axios.get("http://127.0.0.1:3000/blabla/"+uid).then((response) => {
                    return response.data[0].id;
                });
            }

I always get undefined, but I don't understand why. Before the return console everything is correct. I did not find such examples on the Internet.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Evgeny Kulakov, 2017-08-13
@vlad00777

axios.get returns nothing, so undefined.
As an option:

data() {
  return {
    userId: 0
  }
},
mounted () {
this.getUserId(id)
},
methods: {
getUserId(uid) {
                axios.get("http://127.0.0.1:3000/blabla/"+uid).then((response) => {
                    this.userId = response.data[0].id;
                }).catch(error => {
                    console.log(error);
                });
            }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question