美文网首页
Vue学习(4)-父子组件数据传递

Vue学习(4)-父子组件数据传递

作者: 小焲 | 来源:发表于2018-05-05 13:51 被阅读0次

在之前已经学习过如何创建组件和引入组件。这次来学习一下怎么在父子组件之间传递数据,这在实际开发过程中会经常涉及到。
值得注意的是,这里只讨论父子组件之间的数据传递,而两个子组件之间的数据传递可通过父组件做中转,间接进行。

prop

通过在子组件中设置prop,可接收来自父组件的数据。
子组件代码如下:

<template>
  <div style="text-align: center">
    <h3>{{ postTitle }}</h3>
  </div>
</template>

<script>
export default {
  props: ['postTitle']  //props中的参数用于接受父组件的传参
}
</script>

父组件:

<template>
  <!-- 使用子组件 -->
  <todo-item post-title="title"></todo-item>
  <!-- 这里的post-title与子组件中的postTitle相对应,进行传参 -->
</template>

<script>
import todoItem from './components/todo-item' //引入子组件

export default {
  name: 'app1',
  component: [todoItem]  //注册子组件
}
</script>

这是同时prop进行静态传参,我们可以结合v-bind进行动态传参。子组件代码不变,我们来看一下父组件代码:

<template>
  <!-- 这里我们通过v-bind绑定data中的title给子组件中的postTitle -->
  <todo-item v-bind:post-title="title"></todo-item>
</template>

<script>
import todoItem from './components/todo-item'

export default {
  name: 'app1',
  data () {
    return {
      title: '标题:这是一个组件'
    }
  },
  component: [todoItem]
}
</script>

如果使用静态传参,那么传过去的值全都是字符串类型,如果要传递其他类型,请使用v-bind进行动态传参,比如我们要传递数字,父组件代码:

<todo-item post-title="1+1"></todo-item>
...
<todo-item v-bind:post-title="1+1"></todo-item>

大家可以对比一下结果。

$emit

通过触发监听$emit方法我们可以实现子组件使用父组件的方法。
子组件代码

<template>
  <div style="text-align: center">
    <h3>{{ postTitle }}</h3>
    <!--  v-on绑定click事件,被点击时,执行$emit('enlarge-text'),触发enlarge-text方法 -->
    <button  v-on:click="$emit('enlarge-text')"> 放大字体 </button>
  </div>
</template>

<script>
export default {
  props: ['postTitle']
}
</script>

父组件

<template>
  <div :style="{ fontSize: postFontSize + 'em' }">
    <!-- v-on绑定enlarge-text方法,当该方法触发时,执行postFontSize += 0.1 -->
    <todo-item :post-title="title" v-on:enlarge-text="postFontSize += 0.1"></todo-item>
  </div>
</template>

<script>
import todoItem from './components/todo-item'
export default {
  name: 'app1',
  data () {
    return {
      title: '标题:这是一个组件',
      postFontSize: 1
    }
  },
  component: [todoItem]
}
</script>

ref

通过ref我们可以将子组件数据传递给父组件。
子组件不需要做任何改变,只需放置一些数据和方法:

<template>
  <div style="text-align: center">
    <h3>{{ postTitle }}</h3>
  </div>
</template>

<script>
export default {
  props: ['postTitle'],
  data () {
    return {
      message: '信息'
    }
  },
  methods: {
    fn () {
      alert('hello')
    }
  }
}
</script>

在父组件中,给子组件标签添加ref标识,即可在父组件中访问子组件数据。使用this.$refs.标识符名字即可定位到子组件。

<template>
  <div style="text-align: center">
    <!-- 添加ref标识符 -->
    <todo-item ref="child" :post-title="title"></todo-item>
    <button @click="fn">按钮</button>
  </div>
</template>

<script>
import todoItem from './components/todo-item'

export default {
  name: 'app1',
  data () {
    return {
      title: '标题:这是一个组件'
    }
  },
  component: [todoItem],
  methods: {
    fn () {
      //注意这里使用的是refs,而不是ref
      console.log(this.$refs.child.message)
      this.$refs.child.fn()
    }
  }
}
</script>

.sync

.sync 修饰符作为一个编译时的语法糖存在,在使用时它会被扩展为一个自动更新父组件属性的 v-on 监听器。

<comp :foo.sync="bar"></comp>
<!-- 扩展为 -->
<comp :foo="bar" @update:foo="val => bar = val"></comp>
...
//当子组件需要更新 foo 的值时,它需要显式地触发一个更新事件:
this.$emit('update:foo', newValue)

看一下案例:
子组件

<template>
  <div style="text-align: center">
    <h3>{{ postTitle }}</h3>
    <h3>{{ num }}</h3>
    <h3>{{ number }}</h3>
    <button @click="fn2">naniu</button>
  </div>
</template>

<script>
export default {
  props: ['postTitle', 'number'],
  data () {
    return {
      num: this.number
    }
  },
  methods: {
    fn2 () {
      this.$emit('update:number', ++this.num)
    }
  }
}
</script>

父组件:

<template>
  <div style="text-align: center">
    <todo-item ref="child" :post-title="title" :number.sync="num"></todo-item>
  </div>
</template>

<script>
import todoItem from './components/todo-item'

export default {
  name: 'app1',
  data () {
    return {
      title: '标题:这是一个组件',
      num: 1
    }
  },
  component: [todoItem]
}
</script>

点击按钮,可以发现两个数字都发生改变,如果不加.sync修饰符,会发现只有子组件中的数据num 发生改变,而来自父组件的数字number不会改变。

相关文章

  • #搭建Vue+TypeScript项目(五)

    vue组件 组件中使用typescript 主文件 父子组件传递数据 子文件lefebar

  • vue学习

    vue中的事件传递 父子组件传值通过props传递,父组件 :name=“name(父数据)”子组件 props内...

  • Vue的组件通信

    Vue的父子通信问题 参考文档 : Vue 组件组合 使用 props传递数据 用v-on 绑定事件 原理: 父子...

  • 到了Vue2.x有哪些变化?—— 组件通信

    一、父子组件传递数据 默认情况下父子组件的数据是不共享地 推荐一个Vue辅助工具 —— vue-devtool下载...

  • Vue如何实现组件通信?

    Vue组件通信的三种情况: 父子通信 爷孙通信 兄弟通信 父子通信:父组件使用Prop向子组件传递数据,子组件通过...

  • vue组件如何通信?有几种方式?

    在vue中组件通讯可以分为父子组件通讯和非父子组件通信。父组件通过props的方式向子组件传递数据,而子组件可以通...

  • Vue中组件间传值总结 ------ 2020-05-17

    父子组件间传递数据的方式 1、父组件向子组件传递数据 2、子组件向父组件传递数据 3、父子组件相互传递同一数据的两...

  • vue 事件总线EventBus的概念、使用以及注意点

    vue组件中的数据传递最最常见的就是父子组件之间的传递。父传子通过props向下传递数据给子组件;子传父通过$em...

  • vue组件通信(父子)

    在 Vue 中,父子组件的关系可以总结为 prop 向下传递,事件向上传递。父组件通过 prop 给子组件下发数据...

  • Vue组件传值及页面缓存问题

    一、父子组件传值 基本概念在Vue中,父子组件间的数据流向可以总结为prop向下传递,事件向上传递,即父组件通过p...

网友评论

      本文标题:Vue学习(4)-父子组件数据传递

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