Vue父组件如何通过长尾词获取子组件中的变量?

2026-04-08 17:381阅读0评论SEO资讯
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计681个文字,预计阅读时间需要3分钟。

Vue父组件如何通过长尾词获取子组件中的变量?

在Vue项目中,日常开发中务必将功能性组件抽离出来,这种结构会减少父子组件之间的依赖,降低组件间的耦合。但这样做也可能导致不同组件间需要互相引用其值,带来值得注意的问题。之前提到过,可以通过ref来让父组件引用子组件。

在vue项目日常开发中,难免要把功能性组件抽离出来,这样结构就会出现父子组价,兄弟组件等,但是这样就会涉及到不同组件需要互相使用其中的值得问题。

之前有说过通过ref来让父组件操作子组件,并且传值,那么我们今天来详细看看。

案例一:点击父组件的按钮,操作子组件显示

注:可以通过获取id/class来操作,这里我就不介绍这种方法了,至于jquery的话,在vue中还是慎用。

介绍:这里通过给子组件绑定ref属性,引号命名自定义,然后父组件通过 this.$refs.名字 就可以操作子组件的元素,以改变它的样式等。

Vue父组件如何通过长尾词获取子组件中的变量?

<template> <div class="DbSource-box"> <el-button type="primary" icon="" class="addBtn" @click="addDbSource()">新增</el-button> <db-source-add ref="addAlert" v-on:init="init"></db-source-add> </div> </template> <script> import DbSourceAdd from "../components/DbSourceManager/DbSourceAdd"; export default { name: "DbSourceManager", components: {DbSourceAdd}, methods: { // 点击新增按钮,弹出新增数据源的弹框 addDbSource(){ this.$refs.addAlert.$el.style.display = "block"; }, } } </script>

案列二:获取子组件data中的变量

介绍:

父组件:

这里通过给子组件绑定ref属性,引号中的命名自定义,然后父组件通过 this.$refs.名字.变量名就可以获得子组件中的值

<template> <div class="DbSource-box"> <el-button type="primary" icon="" class="selectBtn" @click="deleteSelectDbSource()">批量删除</el-button> <db-source-table ref="getSelectData" :Data="Data" v-on:init="init"></db-source-table> </div> </template> <script> import DbSourceTable from "../components/DbSourceManager/DbSourceTable"; export default { name: "DbSourceManager", components: {DbSourceTable}, methods: { // 删除选中的数据源(批量删除) deleteSelectDbSource(){ console.log(this.$refs.getSelectData.multipleSelection) }, } } </script>

子组件:

<template> <div class="table-box"> </div> </template> <script> export default { name: "DbSourceTable", props:["Data"], data(){ return { multipleSelection:[], pagesize: 3, currpage: 1, currId:"" } } </script>

好了,以上就是父组件获取子组件的值并且操作子组件的方法。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持易盾网络。

标签:变量vue

本文共计681个文字,预计阅读时间需要3分钟。

Vue父组件如何通过长尾词获取子组件中的变量?

在Vue项目中,日常开发中务必将功能性组件抽离出来,这种结构会减少父子组件之间的依赖,降低组件间的耦合。但这样做也可能导致不同组件间需要互相引用其值,带来值得注意的问题。之前提到过,可以通过ref来让父组件引用子组件。

在vue项目日常开发中,难免要把功能性组件抽离出来,这样结构就会出现父子组价,兄弟组件等,但是这样就会涉及到不同组件需要互相使用其中的值得问题。

之前有说过通过ref来让父组件操作子组件,并且传值,那么我们今天来详细看看。

案例一:点击父组件的按钮,操作子组件显示

注:可以通过获取id/class来操作,这里我就不介绍这种方法了,至于jquery的话,在vue中还是慎用。

介绍:这里通过给子组件绑定ref属性,引号命名自定义,然后父组件通过 this.$refs.名字 就可以操作子组件的元素,以改变它的样式等。

Vue父组件如何通过长尾词获取子组件中的变量?

<template> <div class="DbSource-box"> <el-button type="primary" icon="" class="addBtn" @click="addDbSource()">新增</el-button> <db-source-add ref="addAlert" v-on:init="init"></db-source-add> </div> </template> <script> import DbSourceAdd from "../components/DbSourceManager/DbSourceAdd"; export default { name: "DbSourceManager", components: {DbSourceAdd}, methods: { // 点击新增按钮,弹出新增数据源的弹框 addDbSource(){ this.$refs.addAlert.$el.style.display = "block"; }, } } </script>

案列二:获取子组件data中的变量

介绍:

父组件:

这里通过给子组件绑定ref属性,引号中的命名自定义,然后父组件通过 this.$refs.名字.变量名就可以获得子组件中的值

<template> <div class="DbSource-box"> <el-button type="primary" icon="" class="selectBtn" @click="deleteSelectDbSource()">批量删除</el-button> <db-source-table ref="getSelectData" :Data="Data" v-on:init="init"></db-source-table> </div> </template> <script> import DbSourceTable from "../components/DbSourceManager/DbSourceTable"; export default { name: "DbSourceManager", components: {DbSourceTable}, methods: { // 删除选中的数据源(批量删除) deleteSelectDbSource(){ console.log(this.$refs.getSelectData.multipleSelection) }, } } </script>

子组件:

<template> <div class="table-box"> </div> </template> <script> export default { name: "DbSourceTable", props:["Data"], data(){ return { multipleSelection:[], pagesize: 3, currpage: 1, currId:"" } } </script>

好了,以上就是父组件获取子组件的值并且操作子组件的方法。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持易盾网络。

标签:变量vue