美文网首页
vue组件间的传值方式及方法调用汇总

vue组件间的传值方式及方法调用汇总

作者: 唏嘘的码农 | 来源:发表于2022-11-07 14:27 被阅读0次

1.传值

a.父组件传子组件

方法一:

父页面:

<myReportContent v-if="contentState==1" :paramsProps='paramsProps' @back="back" :reportId="reportId" :reportTypex="reportTypex"></myReportContent>

import myReportContent from './myReportContent.vue'

components: {

  myReportContent

}

子页面:

props: {

  reportTypex: {

    type: String

  },

  reportId: {

    type: String

  },

  paramsProps:{

    type:Object,

    default:{}

  }

},

 方法二:

父组件

  provide:{

    nameGet: 'ab888'

  },

子组件

inject:['nameGet'],

data () {

  return {

    msg: 'componentA',

    amount: 1,

    name: this.nameGet

  }

},

b.子组件传父组件

方法一:(也是子组件调用父组件方法的案例)

父组件

<componentb @backParent='backParent'></componentb>

import componentb from 'components/componentB'

components: {

      componentb

}

backParent(amount){

      console.log('获取子组件传过来的数量:' + amount)

}

子组件

changeDataMsg(){

  this.$emit('backParent',this.amount)

  // this.$router.push({path:'/'})

}

c.兄弟组件之间传值

方法一(a改变,b也跟着改变-----------------a传值给b):

创建一个独立的eventVue.js

import Vue from 'vue'

export default new Vue

父组件

  <componenta></componenta>

  <componentb></componentb>

import componenta from 'components/componentA'

import componentb from 'components/componentB'

components: {

  componenta,

  componentb

},

兄弟组件a

<h1>{{ amount }}</h1>import eventVue  from '@/until/eventVue.js'

  data () {

    return {

      msg: 'componentA',

      amount: 1

    }

  }

    changeDataMsg(){

      let amount = this.amount + 1

      eventVue.$emit('myfun',amount)

      this.amount = amount

    }

兄弟组件b

<h1>{{ amount }}</h1><br>

import eventVue  from '@/until/eventVue.js'

    changeDataMsg(){

      eventVue.$on('myfun',(msg)=>{

        this.amount = msg

      })

    }

 created(){

     this.changeDataMsg()

  }

 方法二(b改变,a也跟着改变-----------------b传值给a):

父组件

<componenta ref="childa"></componenta>

<componentb @backParent='backParent' ></componentb>

    backParent(number){

      this.$refs.childa.changeDataNumber(number)

    },

兄弟组件b

  <button @click="backp">backp</button>

  <h1>{{ number }}</h1>

data () {

  return {

    number: 2.1

  }

},

  backp(){

    let number = this.number + 1

    this.$emit('backParent',number)

    this.number = number

  },

兄弟组件a

  <h1>{{ number }}</h1>

data () {

  return {

    number: 9.1,

  }

},

  changeDataNumber(number){

    this.number = number

  }

d.父组件的父组件给孙组件传值(爷爷------>孙子)

2.方法调用

a.父组件调用子组件方法

 方法一:

<h1>{{nameG}}<button @click="parentF">父组件按钮</button></h1>

<componenta ref="childa"></componenta>

parentF(){

    this.$refs.childa.changeDataMsg()

}

子组件

changeDataMsg(){

  console.log('父组件调用子组件方法:ref')

}

 方法二:

b.子组件调用父组件方法

方法一:见1中b的方法一

方法二:

父组件

parentFun(){

      console.log('子组件调用父组件方法:$parent') <br>}

子组件

changeDataMsg(){

      this.$parent.parentFun()

 }

方法三:

父组件

<componentb @backParent='backParent' :parentFuntion="parentFuntion"></componentb>    parentFuntion(){

      console.log('子组件调用父组件方法:props')

    }

子组件

    changeDataMsg(){

      this.parentFuntion()

    }

【https://www.cnblogs.com/cx709452428/p/10616983.html】

相关文章

  • 2019-03-13

    vue父子组件传值,(父组件向子组件传值用prop ,子组件向父组件传值:子组件调用父组件方法值以参数的方式传递)...

  • 2019-03-13

    vue父子组件传值,(父组件向子组件传值用prop ,子组件向父组件传值:子组件调用父组件方法值以参数的方式传递)...

  • 1.14、vue 父子组件的传值

    十四、vue 父子组件的传值 6.子组件调用父组件的方法

  • Vue和React组件通信的总结

    在现代的三大框架中,其中两个Vue和React框架,组件间传值方式有哪些? 组件间的传值方式 组件的传值场景无外乎...

  • vue 6种通信总结①

    常用vue通信大概有这几种方式进行: 组件间的父子之间的传值 组件间的子父之间的传值 非组件间的组件间的传值(简称...

  • vue使用ref调用子组件方法,数据问题

    问题:在vue父子组件传值过程中,使用ref去调用子组件方法,没有在子组件中使用watch监听来调用调用子组件方法...

  • Vue.js组件间调用及传值 2020最新更新

    Vue.js中,对于父子组件、平级组件间的事件触发、传值等。 调用 父组件 子组件 子组件调用父组件 子组件 触发...

  • 2020-03月前端面试题

    vue相关 vue父子组件传值方式有哪些? 兄弟组件间如何传值? vuex是用来干什么的? vuex核心模块有哪些...

  • vue组件间参数与事件传递

    父组件向子组件传值 以及父组件调用子组件方法 子组件向父组件传值 以及子组件触发调用父组件方法

  • 前端基础搬运工-VUE模块

    十、VUE模块 基础部分 1. Vue组件间传值 答: -[ ] 1.父子之间的传值 父组件向子组件传值通过p...

网友评论

      本文标题:vue组件间的传值方式及方法调用汇总

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