• vue2组件通信中的一些拓展(props,emit,ref父子双向传参)


    说明

    我上一篇文章中基本对vue所有的数据通信方法进行了一个整理归纳。

    其实我并没有像传统的那样去罗列,比如父传子有props,ref,子传父为emit,兄弟用$bus等等。

    因为在我的实际练习和业务开发中,props,emit,ref等可以实现父子数据互传,这里就涉及一个比较重要的编程思维,函数式编程。

    当我们使用props传递一个函数呢,那么子组件调用这个函数时,将参数传入这个函数的调用,那么在父组件中定义的函数不就可以直接获取到了子组件中数据了吗。

    其余的两个也是同理,记住,传参有两种,一种是常规变量,一种是函数,传递什么决定了你的功能。

    看代码:

    props实现父子双向传参:

    父组件

    1. <template>
    2. <div>
    3. <p>父组件数字值1:{{ numValue1 }} <el-button @click="addNum">增加el-button>p>
    4. <p>获取子组件的值:{{ numValue2 }}el-button>p>
    5. <hr />
    6. <PropsChild :numValue1="numValue1" :numValue2="getChildValue">PropsChild>
    7. div>
    8. template>
    9. <script>
    10. import PropsChild from './child.vue'
    11. export default {
    12. name: 'propsTest',
    13. components: {
    14. PropsChild
    15. },
    16. data() {
    17. return {
    18. numValue1: 0,
    19. numValue2: '父组件默认的值'
    20. }
    21. },
    22. methods: {
    23. addNum() {
    24. this.numValue1++
    25. },
    26. getChildValue(childValue) {
    27. console.log(childValue, '???子组件的数值')
    28. this.numValue2 = childValue
    29. },
    30. getChildValueFun() {
    31. }
    32. }
    33. }
    34. script>
    35. <style lang="less">style>

    子组件

    1. <template>
    2. <div>
    3. <p>子组件数值:{{ numValue1 }}p>
    4. <el-input v-model="value" @input="numValue2" class="inputBox">el-input>
    5. div>
    6. template>
    7. <script>
    8. export default {
    9. name: 'propsChild',
    10. props: {
    11. numValue1: {
    12. type: Number,
    13. require: true,
    14. default: 0
    15. },
    16. numValue2:{
    17. type:Function,
    18. default:()=>{}
    19. }
    20. },
    21. data(){
    22. return{
    23. value:'子组件本身的值'
    24. }
    25. }
    26. }
    27. script>
    28. <style lang="less">
    29. .inputBox{
    30. width:300px;
    31. margin-top:16px;
    32. }
    33. style>

    $emit实现父子双向传参

    父组件

    1. <template>
    2. <div>
    3. 父组件
    4. <el-input v-model="value" @input="changeInput">el-input>
    5. <hr>
    6. <EmitChild @getChildValue="getChildValue">EmitChild>
    7. div>
    8. template>
    9. <script>
    10. import EmitChild from './child.vue'
    11. export default{
    12. name:'emitTest',
    13. data(){
    14. return{
    15. value:'父组件默认的值',
    16. childFun:()=>{}
    17. }
    18. },
    19. components:{
    20. EmitChild
    21. },
    22. mounted(){
    23. },
    24. methods:{
    25. getChildValue(childValue){
    26. if(typeof childValue === 'function'){
    27. this.childFun = childValue
    28. }else{
    29. this.value = childValue
    30. }
    31. },
    32. changeInput(value){
    33. this.childFun(value)
    34. }
    35. }
    36. }
    37. script>
    38. <style lang="less">
    39. style>

    子组件

    1. <template>
    2. <div>
    3. 子组件
    4. <el-input v-model="value" @input="changeValue">el-input>
    5. div>
    6. template>
    7. <script>
    8. export default{
    9. name:'emitChild',
    10. data(){
    11. return{
    12. value:'子组件默认的值'
    13. }
    14. },
    15. mounted(){
    16. this.$emit('getChildValue',this.emitFun)
    17. },
    18. methods:{
    19. changeValue(){
    20. this.$emit('getChildValue',this.value)
    21. },
    22. emitFun(value){
    23. this.value = value
    24. }
    25. }
    26. }
    27. script>
    28. <style lang="less">
    29. style>

    $ref实现父子双向传参

    父组件

    1. <template>
    2. <div>
    3. <p>父组件的值:{{ value }} <el-button @click="addNum">增加el-button> p>
    4. <hr>
    5. <Child ref="child">Child>
    6. div>
    7. template>
    8. <script>
    9. import Child from './child.vue'
    10. export default{
    11. name:'refTest',
    12. components:{
    13. Child
    14. },
    15. data(){
    16. return{
    17. value:0
    18. }
    19. },
    20. mounted(){
    21. this.$refs.child.getFun(this.funHandler)
    22. },
    23. methods:{
    24. addNum(){
    25. this.value++
    26. this.$refs.child.getValue(this.value)
    27. },
    28. funHandler(value){
    29. this.value = value
    30. }
    31. }
    32. }
    33. script>

    子组件

    1. <template>
    2. <div>
    3. 子组件的值{{ value }}
    4. <el-button @click="add">增加el-button>
    5. div>
    6. template>
    7. <script>
    8. export default{
    9. name:'refTest',
    10. data(){
    11. return{
    12. value:0,
    13. funHandler:()=>{}
    14. }
    15. },
    16. methods:{
    17. getValue(val){
    18. this.value = val
    19. },
    20. getFun(funHandler){
    21. this.funHandler = funHandler
    22. },
    23. add(){
    24. this.value ++
    25. this.funHandler(this.value)
    26. }
    27. }
    28. }
    29. script>

    结束语

    虽然在开发中,大部分人都会按照既定思维,props和ref父传子,emit子传父,但是我们是开发者,需要有更多的思维在里面,你可以不用,但是你需要去思考,去尝试。

    vue和react等框架本身就大量使用了函数式编程的思想,作为使用者的我们,也要同样的将这种思想运用进去,才会有更多优质的代码,才回提升自己。

  • 相关阅读:
    Cadence OrCAD Capture “网络名”相同,但是未连接或连接错误的解放方案之nodename的用法
    Java代码实现贪吃蛇游戏
    设计模式-策略模式
    旅游攻略APP外包开发功能
    通配符指什么呢?
    css设置文本溢出隐藏...
    Linux常见的命令操作
    35. 搜索插入位置
    MySQL与ES数据同步的四种方案及实践演示
    【Linux】自动化构建工具-make/Makefile&&第一个小程序
  • 原文地址:https://blog.csdn.net/m0_54741495/article/details/134504650