• 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
    },
    
  • 相关阅读:
    附加:CSS大全
    HTML 三
    HTML 二
    HTML 一
    20150106--SQL事务操作+触发器二
    20150106--SQL事务操作+触发器一
    20141229 mysql基本操作二
    Oracle基础知识
    JDBC事务
    jsp内置/隐式对象(9个)与el表达式
  • 原文地址:https://www.cnblogs.com/lvonve/p/14180257.html
Copyright © 2020-2023  润新知