• Vue2/3 父子组件传值


    Vue2

    1.子组件获取父组件

    1. 1. 我是父组件传的参数{{ msg }}
  • <script>
  • export default{
  • // 1.接收父组件的值
  • props:{
  • msg:String
  • }
  • }
  • script>
  • 1.1.父组件传给子组件

    1. // 1.1传给子组件值 <子组件 :msg="">
    2. <Son :msg="传值">Son>
  • <script>
  • // 引入子组件
  • import Son from './Son.vue'
  • export default {
  • components: {
  • // 1.1子组件
  • Son
  • }
  • }
  • script>
  • 2.1.父组件调用子组件方法

    1. // 2.2.@opParent必须和子组件一样
    2. <Son ref="SonRef" @opParent="parentMethod">Son>
    3. // 2.1.方法
    4. <button @click="opSon">button>
  • // 引入子组件
  • import Son from './Son.vue'
  • export default {
  • components: {
  • // 子组件
  • Son
  • },
  • methods: {
  • opSon() {
  • // 2.1.调用子组件方法
  • tshi.$refs.sonRef.ParentOpSon("我是父组件传入的参数");
  • },
  • // 2.2.调用父组件方法
  • parentMethod (e) {
  • console.log('刷新')
  • }
  • }
  • }
  • 2.2.子组件调用父组件方法                                                                   

    1. 我是父组件传的参数{{ msg }}
  • <script>
  • export default{
  • // 接收父组件的值
  • props: {
  • msg:String
  • },
  • methods: {
  • // 2.1.子组件方法
  • ParentOpSon (parentMsg) {
  • console.log(parentMsg);
  • }
  • // 2.2.调用父组件方法
  • opParent(){
  • this.$emit("opParent","我是子组件调用父组件方法的参数");
  • }
  • }
  • }
  • script>
  • Vue3

    1.子组件获取父组件

    1. {{ msg }}
    2. <script>
    3. import { ref, defineProps } from 'vue'
    4. const props = defineProps({
    5. msg: {
    6. type: String,
    7. default: ""
    8. }
    9. })
    10. script>

    1.1父组件传给子组件

    1. <Son :msg="myMsg">Son>
    2. <script>
    3. import Son from './Son.vue'
    4. import { ref } from 'vue'
    5. const myMsg = ref("我是父组件传的值");
    6. script>

    2.1父组件调用子组件方法

    1. <script>
    2. import { ref, defineProps, defineExpose } from 'vue'
    3. const props = defineProps({
    4. msg: {
    5. type: String,
    6. default: ""
    7. }
    8. })
    9. const msg2 = ref("");
    10. const ParentOpSon = (parentMsg) => {
    11. console.log("父组件的参数", parentMsg);
    12. }
    13. // 导出方法父组件才能用
    14. defineExpose({
    15. ParentOpSon
    16. })
    17. script>
    18. <div>
    19. <Son ref="sonRef">Son>
    20. <button @click="opSon">传给子组件方法button>
    21. div>
    22. <script>
    23. import Son from './Son.vue'
    24. import { ref } from 'vue'
    25. const sonRef = ref();
    26. const opSon = () => {
    27. sonRef.value.ParentOpSon("传给子组件的参数");
    28. }
    29. script>

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

    1. <div>
    2. <button @click="opParent">调用父组件方法button>
    3. div>
    4. <script>
    5. const emit = defineEmits();
    6. // 子组件调用父组件方法
    7. const opParent = () => {
    8. emit("opParent","调用父组件方法");
    9. }
    10. script>
    11. <div>
    12. <Son ref="sonRef" @opParent="parentMethod ">Son>
    13. <button @click="opSon">传给子组件方法button>
    14. div>
    15. <script>
    16. import Son from './Son.vue'
    17. import { ref } from 'vue'
    18. const parentMethod = (e) => {
    19. console.log(e);
    20. }
    21. script>
  • 相关阅读:
    使用股票量化交易接口需要具备怎么样的心态
    G2plot图表在Vue中使用-获取新数据后二次渲染图表-案例
    【软考软件评测师】第三十二章 数据库系统基础知识
    二叉树根节点到叶子节点的所有路径和
    AQS之基础分析 (一)
    从初级程序员到CEO,汤鹏与时代碰撞出的那些“火花”
    FAST-LIO(二):程序运行&代码注释
    栈的多种C语言实现 编程
    什么是浏览器的同源策略(same-origin policy)?它对AJAX有什么影响?
    kubernetes 概述
  • 原文地址:https://blog.csdn.net/weixin_55666891/article/details/133039981