Vue+ElementUi实现点击表格中链接进行页面跳转和路由

本文详细介绍了如何在Vue中实现表格中点击ASIN进行页面跳转到亚马逊购物界面,以及通过<router-link>进行路由切换并传递参数。在表格列定义中,利用<template>和<el-link>结合scope.row属性实现跳转,同时用<router-link>和:name属性完成路由切换,并在目标组件中通过this.$route.params获取传递的参数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、页面跳转,先看效果

 点击表格中的asin会跳转到亚马逊购物界面

怎么做的呢,直接上代码

<el-table-column
        prop="asin"
        label="asin"
        width="150"
        fixed>
        <template slot-scope="scope">
          <el-link :href="scope.row.url" type="primary" target="_blank">{{scope.row.asin}}</el-link>
        </template>
      </el-table-column>

asin那一列通过<template>标签把scope传进去,scope是包含这一行的信息的,在标签里面使用<el-link>标签配合数据里面的url实现页面跳转,获取某个属性可以通过scope.row.属性名 获取

2、路由切换加传参数,先看效果

 点击标题

可以看到路由切换到产品分析了,并且asin数据也传递过去了

实现直接看代码(需要注意的是需要传参的话只能使用name属性,使用path属性跳转是不能传递参数的)

<el-table-column
        prop="title"
        label="标题"
        width="150"
        :show-overflow-tooltip="true">
        <template slot-scope="scope">
          <router-link :to= "{name: 'productsAnalysis',params: {asin: scope.row.asin }}">
            <span>
              {{scope.row.title}}
            </span>
          </router-link>
        </template>
      </el-table-column>

可以看到路由切换与页面跳转类似,都是通过<template>标签实现的,区别就是<router-link>里面直接

{{scope.row.title}}不好使,需要借助<span>标签实现内容展示

路由切换使用路由名字

productsAnalysis,点击标题时路由器会找到productsAnalysis路由,并且把参数params传过去,看一下我的路由怎么实现的吧
{
    path: '/console',
    component: Layout,
    redirect: '/console/productsAnalysis',
    name: 'console',
    meta: { title: '销售', icon: 'el-icon-s-help' },
    children: [
      {
        path: 'productsAnalysis',
        name: 'productsAnalysis',
        component: () => import('@/views/products/productsAnalysis'),
        meta: { title: '产品分析', icon: 'table' }
      },
      {
        path: 'productPerspective',
        name: 'productPerspective',
        component: () => import('@/views/products/productPerspective'),
        meta: { title: '产品透视', icon: 'table' }
      }
    ]
  },

可以看到路由名为productsAnalysis的会跳转到

@/views/products/productsAnalysis组件

 看一下productsAnalysis组件怎么拿到参数的

<template>
<dev>
  <h1>ProductsAnalysis</h1>
  <h1>{{asin}}</h1>
</dev>
</template>

<script>
import router from '@/router'
export default {
  data(){
    return{
      asin: null
    }
  },
  created() {
    this.asin = this.$route.params.asin
  }
}
</script>

<style scoped>

</style>

直接

this.$route.params.asin就能拿到路由传过来的参数

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值