zoukankan      html  css  js  c++  java
  • vue中子组件使用$emit传值的种种情况

    1、 子组件不传递参数,父组件也不接受参数

    // 子组件
    this.$emit('test')
    // 父组件
    @test='test'
    
    test() {
        
    }
    

    2、 子组件传递一个参数,父组件接收时不带形参

    // 子组件
    this.$emit('test','哈哈')
    // 父组件
    @test='test'
    
    test(param) {
    	console.log(param); // 哈哈
    },
    

    3、 子组件传递多个参数,父组件接收时需要使用arguments作为形参。arguments是一个数组。

    // 子组件
    this.$emit('test','哈哈1','哈哈2')
    // 父组件
    @test='test(arguments)'
    
    test(params) {
    	console.log(params[0]); // 哈哈1
     	console.log(params[1]); // 哈哈2
    },
    

    4、 子组件传递一个参数,父组件在接收参数时还加上了自己的一个属性,那么父组件需要使用形参$event 来替代子组件传递的参数。

    // 子组件
    this.$emit('test','哈哈')
    // 父组件
    @test='test('呵呵',$event)'
    test(myparam,param) {
         console.log(myparam); // 呵呵
         console.log(param); // 哈哈
    },
    

    5、 子组件传递多个参数时,父组件在接收参数时还加上了自己的一个属性,那么父组件需要使用形参arguments 来替代子组件传递的多个参数。

    // 子组件
    this.$emit('test','哈哈1','哈哈2')
    // 父组件
    @test='test(arguments,'哈哈3')'
    test(params,myparam) {
         console.log(params[0]); // 哈哈1
         console.log(params[1]); // 哈哈2
         console.log(myparam); // 哈哈3
    },
    
  • 相关阅读:
    [转]Linux(Ubuntu)下如何安装JDK
    第一个MICO CORBA demo实录
    解决/usr/bin/ld: cannot find -lssl
    使用adb shell 进入手机修改文件的权限
    解决某些Android Permission denied
    Java 8新特性终极指南
    Win10系统出问题?简单一招即可修复win10!
    运行时数据区
    linux下vi命令大全
    关于java中final关键字与线程安全性
  • 原文地址:https://www.cnblogs.com/lvonve/p/14180257.html
Copyright © 2011-2022 走看看