vue组件传值方式总结

在这里插入图片描述

1. 父组件给子组件传值

示例
子组件

// children
<template>
    <section>
        父组件传过来的消息是:{{myMsg}}
    </section>
</template>

<script>
export default {
   name: "Children",
   components: {},
   props:{
     msg: {
   		type: String,
   		default: 'HELLO WORLD'
	 }
  },
  data() {
       return {
           myMsg:this.msg
       }
   }
}
</script>

父组件

<template>
  <div class="parent">
    <Children :msg="message"></Children>
  </div>
</template>

<script>
import Children from '../components/Children'

export default {
  name: 'Parent',
  components: {
    Children
  },
  data() {
    return {
      message:'hello world'
	}
  },
}
</script>

2.子组件给父组件传值

父组件

// parent
<template>
  <div id="app">
    <children @getChildrenVal="getChildrenVal"></children>
  </div>
</template>

<script>
import Children from '../src/components/children.vue'

export default {
  name: 'App',
  components: {
    Children
  },
  methods: {
    getChildrenVal(value) {
      console.log(value)
    }
  }
}
</script>

子组件

// children
<template>
  <div>
    <button @click="toParent()"></button>
  </div>
</template>

<script>
export default {
  name: 'children',
  data() {
    return {
      msg: 1
    }
  },
  methods: {
    toParent() {
      this.msg++
      this.$emit('getChildrenVal', this.msg)
    }
  }
}
</script>

3.兄弟组件中使用EventBus传参

父组件

// parent
<template>
  <div id="app">
    <children-one></children-one>
    <children-two></children-two>
  </div>
</template>

<script>
import ChildrenOne from '../src/components/children.vue'
import ChildrenTwo from './components/childrenTwo.vue'

export default {
  name: 'App',
  components: {
    ChildrenOne,
    ChildrenTwo
  }
}
</script>

EventBus.js

// EventBus.js
import Vue from 'vue'
export default new Vue()

子组件一

// childrenOne
<template>
  <div>
    <button @click="pushMsg">组件一传参给组件二</button>
  </div>
</template>

<script>
import eventBus from '../utils/eventBus.js'

export default {
  name: 'childrenOne',
  data() {
    return {
      msg: 1
    }
  },
  methods: {
    pushMsg() {
      this.msg++
      eventBus.$emit('pushMsg', this.msg)
    }
  }
}
</script>

子组件二

// 组件二
<template>
  <div>
    组件二{{msg}}
  </div>
</template>

<script>
import eventBus from '../utils/eventBus.js'

export default {
  name: 'children',
  data() {
    return {
      msg: 1
    }
  },
  mounted() {
    eventBus.$on('pushMsg', (value) => {
      this.msg = value
    })
  }
}
</script>

4. 路由间传值

(1) 使用?传值

// a页面(?后传参)
this.$router.push('/B?name=demo');

//b页面(query接收url上面的参数)
var name = this.$route.query.name

(2) 使用:传值

// 路由配置
{
	path: '/B/:name',
	name: B,
	component: () => import('../views/B.vue')
}
// 通过this.$route.params.name获取

(3) 父子传值

// parent页面
<router-view :name=name></router-view>
// children页面
// 由于name更新之后并没有刷新路由,所以在mounted中无法监听到name的变化,得使用watch监听
props: ['name'],
watch: {
	name(oldVal,newVal) {
		console.log(newVal)
	}
}

5. 通过refs传参

parent页面(通过ref调用子组件的方法并传入参数)

<template>
  <div id="app">
    <button @click="setMsg">改变children的msg值</button>
    <children-one ref="childrenOne"></children-one>
  </div>
</template>

<script>
import ChildrenOne from '../src/components/children.vue'

export default {
  name: 'App',
  components: {
    ChildrenOne,
  },
  methods: {
    setMsg() {
      this.$refs.childrenOne.setMsg('msg值已经被改变')
    }
  }
}
</script>

children页面(通过setMsg函数获取传入的参数值)

<template>
  <div>
    {{ msg }}
  </div>
</template>

<script>
export default {
  name: 'childrenOne',
  data() {
    return {
      msg: 1
    }
  },
  methods: {
    setMsg(value) {
      this.msg = value
    }
  }
}
</script>

6. 通过依赖注入provide和inject给后代

父组件

<template>
  <div id="app">
    <children-one></children-one>
  </div>
</template>

<script>
import ChildrenOne from '../src/components/children.vue'

export default {
  name: 'App',
  components: {
    ChildrenOne,
  },
  provide() {
    return {
      name: 'demo'
    }
  }
}
</script>

子组件

<template>
  <div>
    {{ name }}
  </div>
</template>

<script>
export default {
  name: 'childrenOne',
  inject: ['name']
}
</script>

7.祖传孙使用$attrs

// GrandParent
<template>
  <div id="app">
    <children-one name="xp" age="18" value="123"></children-one>
  </div>
</template>

<script>
import ChildrenOne from '../src/components/children.vue'

export default {
  name: 'App',
  components: {
    ChildrenOne,
  }
}
</script>
// parent
<template>
  <div>
    组件一:{{name}}
    <children-two v-bind="$attrs"></children-two>
  </div>
</template>

<script>
import ChildrenTwo from './childrenTwo.vue'

export default {
  name: 'childrenOne',
  // 如果父组件接收了祖父传过来的name参数,那么这个参数就不会传给子组件,子组件就获取不到这个值
  props: ['name'],
  components: {
    ChildrenTwo
  }
}
</script>
// children
<template>
  <div>
    组件二{{name}} {{age}} {{value}}
  </div>
</template>

<script>
export default {
  name: 'children',
  props: ['name','age','value'],
  data() {
    return {
    }
  },
  mounted() {
  }
}
</script>

8. 孙传祖使用$listeners

GrandParent.vue

// GrandParent
<template>
  <div id="app">
    <children-one @eventOne="eventOne"></children-one>
    {{ msg }}
  </div>
</template>

<script>
import ChildrenOne from '../src/components/children.vue'

export default {
  name: 'App',
  components: {
    ChildrenOne,
  },
  data() {
    return {
      msg: ''
    }
  },
  methods: {
    eventOne(value) {
      this.msg = value
    }
  }
}
</script>

parent.vue

// parent
<template>
  <div>
    <children-two v-on="$listeners"></children-two>
  </div>
</template>

<script>
import ChildrenTwo from './childrenTwo.vue'

export default {
  name: 'childrenOne',
  components: {
    ChildrenTwo
  }
}
</script>

children.vue

// children
<template>
  <div>
    <button @click="setMsg">点击传给祖父</button>
  </div>
</template>

<script>
export default {
  name: 'children',
  methods: {
    setMsg() {
      this.$emit('eventOne', '123')
    }
  }
}
</script>

9. $parent

通过$parent获取父组件的实例,拿到父组件data中的参数

父组件

<template>
  <div>
    <children-two></children-two>
  </div>
</template>

<script>
import ChildrenTwo from './childrenTwo.vue'

export default {
  name: 'childrenOne',
  components: {
    ChildrenTwo
  },
  data() {
    return {
      msg: 'msg'
    }
  }
}
</script>

子组件

<template>
  <div>
    <button @click="getMsg">点击获取父亲传来的参数值</button>
    <p>父组件传来的参数值为:{{ msg }}</p>
  </div>
</template>

<script>
export default {
  name: 'children',
  data() {
    return {
      msg: ''
    }
  },
  methods: {
    getMsg() {
      console.log(this.$parent)
      this.msg = this.$parent.msg
    }
  }
}
</script>

10. sessionStorage

sessionStorage是浏览器中的全局对象,它会在当前页面关闭时被清除,所以我们可以在一个页面中共享一份数据。

sessionStorage操作方法

// 保存数据到 sessionStorage
sessionStorage.setItem('key', 'value');

// 从 sessionStorage 获取数据
sessionStorage.getItem('key');

// 从 sessionStorage 删除保存的数据
sessionStorage.removeItem('key');

// 从 sessionStorage 删除所有保存的数据
sessionStorage.clear();

由于sessionStorage中只能存储字符串类型,如果想存储对象的话需使用JSON.stringify(obj)去转换为json字符串,然后存储到sessionStorage中。
这里给大家推荐个插件good-storage,可直接调用api,将想缓存的数据直接存进去即可。

 import storage from 'good-storage'
 
 // localStorage
 storage.set(key,val) 
 
 storage.get(key, def)
 
 // sessionStorage
 storage.session.set(key, val)
 
 storage.session.get(key, val)

11. Vuex

通过mutation进行state的同步修改,action进行state的异步修改。
在vue组件中使用$state拿取参数,这里就不做过多的描述(如果不是大型项目中建议不使用)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue组件传值Vue.js中非常重要的一个概念,它涉及到父子组件之间的数据传递和通信。下面是一些常见的Vue组件传值面试题及其答案: 1. 请介绍Vue组件之间的父子组件传值方式。 答:Vue组件之间的父子组件传值方式有props和$emit。通过props可以将数据从父组件传递给子组件,子组件通过props接收数据。而$emit则是在子组件中触发一个自定义事件,并通过事件参数将数据传递给父组件。 2. 请介绍Vue组件之间的兄弟组件传值方式。 答:Vue组件之间的兄弟组件传值方式可以通过共享状态管理工具(如Vuex)或者事件总线来实现。使用共享状态管理工具可以在全局定义一个状态,兄弟组件通过读取和修改该状态来进行数据传递。而事件总线则是在Vue实例上定义一个事件中心,兄弟组件通过$emit和$on来进行事件的发布和订阅,从而实现数据传递。 3. 请介绍Vue组件之间的跨级组件传值方式。 答:Vue组件之间的跨级组件传值方式可以通过provide和inject来实现。父级组件通过provide提供数据,然后子孙级组件通过inject来注入数据。这样就可以实现跨级组件之间的数据传递。 4. 请介绍Vue组件之间的非父子组件传值方式。 答:Vue组件之间的非父子组件传值方式可以通过事件总线、Vuex或者localStorage等方式来实现。事件总线和Vuex的方式在前面已经介绍过了,而localStorage则是通过将数据存储在浏览器的本地存储中,然后在其他组件中读取该数据来实现非父子组件之间的数据传递。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值