• 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>
  • 相关阅读:
    jQuery之选择器以及相关基本操作(操作元素属性、操作元素内容、操作元素样式)
    华为机试真题 Python 实现【积木最远距离】【2022.11 Q4 新题】
    使用Go模块进行依赖管理
    华为云云服务器评测 [Vue3 博物馆管理系统] 使用Vue3、Element-plus菜单组件构建轮播图
    Macos文件图像比较工具:Kaleidoscope for Mac
    【VSCode设置单个子文件时不要平级显示】
    使用Python进行数据分析与可视化
    DP31 买卖股票的最好时机(二)、(三)、(四)
    Java电商平台 - API 接口设计之 token、timestamp、sign 具体架构与实现
    PTE考试解析
  • 原文地址:https://blog.csdn.net/weixin_55666891/article/details/133039981