【问题标题】:How to build a VUE link in a method using vue-router如何在使用vue-router的方法中建立VUE链接
【发布时间】:2024-04-23 04:55:01
【问题描述】:

我是 VUE.JS 的新手,我爱上了它!我喜欢 vue-routerrouter-link!他们太棒了!

现在我有一个由来自 axios 的数据填充的表,我想在自定义方法中使用此数据构建一个链接,以使团队名称可点击。

这里是模板:

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

axios返回用于更新表的ID、名称等数据为here

基本上,我需要使用 axios 接收到的数据来更新表中的值。比如:

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

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

但显然它不起作用......

如何建立链接?

【问题讨论】:

  • 您需要动态添加链接到您的标记吗?你不能有条件地隐藏/显示链接吗?
  • 在组件中,我有这个: amd 数据通过作为数据,我需要使用我的帖子中的 router-link 创建链接。

标签: vue.js vue-router


【解决方案1】:

我在列表设置中使用自定义列事件和格式化程序修复了它:

                       {
                        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}`)
                            }
                        }
                    },

另一种解决方案

如果 JSON 代码具有链接而不是表配置,则在 mount() 中添加点击侦听器,并在 JSON HTML 链接中添加格式良好的数据集:

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

听者在这里:

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);
            }
        });
    }

【讨论】:

    【解决方案2】:

    对于您的问题,这可能是较短的解决方案:

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

    【讨论】:

    • 感谢您的建议,但我需要将链接呈现为 JSON (API) 传递的自定义 HTML 代码
    最近更新 更多