简体   繁体   中英

How to build a VUE link in a method using vue-router

I'm new using VUE.JS and I'm in love with it! I love the vue-router and router-link ! They are awesome!

Now I have a table populated by data coming from axios and I would like to build a link using this data in a custom method to have the team name clickable.

Here the template:

  <BootstrapTable :columns="table.columns" :data="table.data" :options="table.options"></BootstrapTable>

Axios returns ID, name and other data used to update the table as here

Basically, I need to update the values in my table using the axios's received data. Something like:

    team: '<a v-bind:href="club/'+team.id+'">'+team.team+'</a>',

or

    team: '<router-link :to="club/'+team.id+'">'+team.team+'</router-link>',

But obviously it dosn't works...

How can a build a link?

I fixed it using custom column event and formatter in columns table setting:

                       {
                        field: 'match',
                        title: 'Match',
                        formatter (value, row) {
                            return `<a href="/matches/${row.pos}">${value}</a>`
                        },
                        events: {
                            'click a': (e, value, row, index) => {
                               e.preventDefault();
                                this.$router.push(`/matches/${row.pos}`)
                            }
                        }
                    },

Another solution :

Just in case of JSON code having links instead of table config is adding click listener in mounted() and a well formatted dataset in JSON HTML link:

   team: "<a href=\"/club/"+team.id+"\" data-to='{\"name\": \"team\",\"params\":{\"teamId\":"+ team.id+"}}'>"+ team.team+"</a> "+userCode

Here the listener:

mounted() {
        window.addEventListener('click', event => {
            let target = event.target;
            if (target && target.href && target.dataset.to) {
                event.preventDefault();
                const url = JSON.parse(target.dataset.to);
                //router.push({ name: 'user', params: { userId: '123' } })
                this.$router.push(url);
            }
        });
    }

This might be shorter solution for your issue :

 routes = [
 {
 component : 'club',
 name : 'club',
 path : '/club/:teamid'
 }
]
 <a @click="$router.push({ name: 'club', params: { teamid: team.id}})">team.team</a>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM