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

子组件

changeDataMsg(){
this.$parent.parentFun()
}

方法三:

父组件

    <componentb @backParent='backParent' :parentFuntion="parentFuntion"></componentb>
parentFuntion(){
console.log('子组件调用父组件方法:props')
}

子组件

    changeDataMsg(){
this.parentFuntion()
}

vue组件间的传值方式及方法调用汇总的更多相关文章

  1. vue组件间通信六种方式(完整版)

    本文总结了vue组件间通信的几种方式,如props. $emit/ $on.vuex. $parent / $children. $attrs/ $listeners和provide/inject,以 ...

  2. vue程序中组件间的传值方式

    vue程序在组件中进行传值有多种方式,这里记录我在项目中使用到的三种: 1. 父组件向子组件传值 2. 子组件向父组件传值 3. 通过路由传参 父组件通过props向子组件传值 在子组件script中 ...

  3. vue组件之间的传值方式

    一.父组件向子组件传值方式 1.1父组件向子组件传数据方式 <!DOCTYPE html> <html lang="en"> <head> &l ...

  4. Vue 组件间的传值(通讯)

    组件之间的通讯分为三种 父给子传 子给父传 兄弟组件之间的通讯 1 父组件给子组件传值 子组件嵌套在父组件内部,父组件给子组件传递一个标识,在子组件内部用props接收,子组件在模板里可以通过{{}} ...

  5. [Props] vue组件间的传值及校验

    基本用法 Prop的基本用法很简单,只需要在子组件的Vue实例中定义该属性并把值设为目标属性的数组即可 Vue.component('child', { ... // 接收message props: ...

  6. vue-learning:31 - component - 组件间通信的6种方法

    vue组件间通信的6种方法 父子组件通信 prop / $emit 嵌套组件 $attrs / $liteners 后代组件通信 provide / inject 组件实例引用 $root / $pa ...

  7. vue组件定义方式,vue父子组件间的传值

    vue组件定义方式,vue父子组件间的传值 <!DOCTYPE html> <html lang="zh-cn"> <head> <met ...

  8. Vue组件间通信6种方式

    摘要: 总有一款合适的通信方式. 作者:浪里行舟 Fundebug经授权转载,版权归原作者所有. 前言 组件是 vue.js 最强大的功能之一,而组件实例的作用域是相互独立的,这就意味着不同组件之间的 ...

  9. Vue中组件间通信的方式

    Vue中组件间通信的方式 Vue中组件间通信包括父子组件.兄弟组件.隔代组件之间通信. props $emit 这种组件通信的方式是我们运用的非常多的一种,props以单向数据流的形式可以很好的完成父 ...

随机推荐

  1. js往标签下插入标签的方法

    js="document.getElementsByClassName('CodeMirror-line')[0].innerHTML = 'xxxxxx'"dr.execute_ ...

  2. LeetCode_100. Same Tree

    100. Same Tree Easy Given two binary trees, write a function to check if they are the same or not. T ...

  3. iOS面试-深拷贝和浅拷贝

    浅copy:实际上的内存只有一份 任何copy都只是指向这个内存的一个引用 深copy:原始数据有一份 每一个copy的对象不再是引用 而是内容大小一样 内存地址不同的独立对象 系统的非容器类对象 c ...

  4. python获取昨日日期

    获取昨日日期oneday = datetime.timedelta(days=1) 一天 day = datetime.datetime.strptime(self.date,'%Y-%m-%d') ...

  5. vue和小程序的相似之处

    小程序参考vue语法,之前做过小程序的,可以逆向思维.1,Vue文件后缀是.vue,vue组件把html<template>.js<script>和css<style&g ...

  6. SC创建服务编写bat脚本

    新建bat脚本,并写入一下文本保存 sc create "DevFast.SupportGPSWarmService" binpath= "%cd%\DevFast.Su ...

  7. Linux下使用Vim粘贴文本错乱问题解决

    在使用vim进行文档操作时,经常需要进行复制粘贴,在粘贴大量代码时,出现行错位等各种错乱,查找问题解决办法: vim进入文件后,先ESC 在出入 :set paste  回车后再按下 i  之后进行粘 ...

  8. 对快速排序的理解以及相关c++代码

    快速排序:在一组数据中,可以将左边的数字当作枢轴(右边也可以),接下来要做的就是,先从右边找到比枢轴小的数, 再从左边找到比枢轴大的数,接着将这两个数进行交换,重复上述步骤找出所有符合条件的数进行交换 ...

  9. git使用中的一些命令及心得

    Git 与 SVN 区别点: 1.Git 是分布式的,SVN 不是:这是 Git 和其它非分布式的版本控制系统,例如 SVN,CVS 等,最核心 的区别. 2.Git 把内容按元数据方式存储,而 SV ...

  10. EXIT(外部中断)控制实验

    实验目的 设计使用外接的按键来作为触发源,使得控制器产生中断,并在中断服务函数中实现控制小灯的亮灭. 按键硬件点路 编程要点 初始化用来产生中断的 GPIO: 初始化 EXTI: 配置 NVIC: 编 ...