美文网首页
vue路由带参进行传值

vue路由带参进行传值

作者: 编程小橙子 | 来源:发表于2020-01-24 01:04 被阅读0次
vue.jpg

先看效果图

image.png
image.png

使用path父组件向子组件传值

parint

// 使用path父组件向子组件传值
 /**
 * 注意:
 * 1.使用path传值,path和query搭配
 * 2.传值使用$router
 */
this.$router.push({ path: 'page2', query: { tableData: this.tableData } })
<template>
  <div>
    <el-button type="primary" @click="handleClick">点击</el-button>
  </div>
</template>

<script>
export default {
  data() {
    return {
      tableData: [
        {
          date: '2016-05-02',
          name: '王小虎',
          address: '上海市普陀区金沙江路 1518 弄'
        },
        {
          date: '2016-05-04',
          name: '王小虎',
          address: '上海市普陀区金沙江路 1517 弄'
        },
        {
          date: '2016-05-01',
          name: '王小虎',
          address: '上海市普陀区金沙江路 1519 弄'
        },
        {
          date: '2016-05-03',
          name: '王小虎',
          address: '上海市普陀区金沙江路 1516 弄'
        }
      ]
    };
  },
  methods: {
    handleClick() {
      // 使用path父组件向子组件传值
      /**
       * 注意:
       * 1.使用path传值,path和query搭配
       * 2.传值使用$router
       */
      this.$router.push({ path: 'page2', query: { tableData: this.tableData } });
    }
  }
};
</script>

<style lang="scss" scoped></style>

子组件接收父组件传递的值

child

// 接收父组件使用path传值
 /**
 * 注意:
 * 1.接收的时候是$route,千万记住不能加r
 * 2.这里使用query
 */
this.$route.query.tableData
<template>
  <div>
    <el-table :data="tableData" style="width: 100%">
      <el-table-column prop="date" label="日期" width="180"> </el-table-column>
      <el-table-column prop="name" label="姓名" width="180"> </el-table-column>
      <el-table-column prop="address" label="地址"> </el-table-column>
    </el-table>
  </div>
</template>

<script>
export default {
  data() {
    return {
      tableData: []
    };
  },
  created() {
    this.getListAll();
  },
  methods: {
    getListAll() {
      // 接收父组件使用path传值
      /**
       * 注意:
       * 1.接收的时候是$route,千万记住不能加r
       * 2.这里使用query
       */
      var tableData = this.$route.query.tableData;
    }
  }
};
</script>

<style lang="scss" scoped></style>

使用name进行传值

/**
 * 注意:
 * 1.使用name传值,name和params搭配
 * 2.传值同上使用$router
 */
  this.$router.push({ name: 'page2', params: { tableData: this.tableData } });
<template>
  <div>
    <el-button type="primary" @click="handleClick">点击</el-button>
  </div>
</template>

<script>
export default {
  data() {
    return {
      tableData: [
        {
          date: '2016-05-02',
          name: '王小虎',
          address: '上海市普陀区金沙江路 1518 弄'
        },
        {
          date: '2016-05-04',
          name: '王小虎',
          address: '上海市普陀区金沙江路 1517 弄'
        },
        {
          date: '2016-05-01',
          name: '王小虎',
          address: '上海市普陀区金沙江路 1519 弄'
        },
        {
          date: '2016-05-03',
          name: '王小虎',
          address: '上海市普陀区金沙江路 1516 弄'
        }
      ]
    };
  },
  methods: {
    handleClick() {
      // 使用name父组件向子组件传值
      /**
       * 注意:
       * 1.使用name传值,name和params搭配
       * 2.传值同上使用$router
       */
      // this.$router.push({ name: 'page2', params: { tableData: this.tableData } });
    }
  }
};
</script>

<style lang="scss" scoped></style>

使用params接收父组件传递的值

// 接收父组件使用name传值
 /**
 * 注意:
 * 1.接收的时候是$route,千万记住不能加r
 * 2.这里使用params
 */
  var tableData = this.$route.params.tableData;
<template>
  <div>
    <el-table :data="tableData" style="width: 100%">
      <el-table-column prop="date" label="日期" width="180"> </el-table-column>
      <el-table-column prop="name" label="姓名" width="180"> </el-table-column>
      <el-table-column prop="address" label="地址"> </el-table-column>
    </el-table>
  </div>
</template>

<script>
export default {
  data() {
    return {
      tableData: []
    };
  },
  created() {
    this.getListAll();
  },
  methods: {
    getListAll() {
      // 接收父组件使用name传值
      /**
       * 注意:
       * 1.接收的时候是$route,千万记住不能加r
       * 2.这里使用params
       */
      var tableData = this.$route.params.tableData;
      this.tableData = tableData;
    }
  }
};
</script>
<style lang="scss" scoped></style>

相关文章

网友评论

      本文标题:vue路由带参进行传值

      本文链接:https://www.haomeiwen.com/subject/tctzzctx.html