vue ts 子组件使用Emit给父组件传值

news/2024/5/20 5:08:08 标签: vue, ts

vue20_ts_Emit_0">vue2.0 ts 子组件使用Emit给父组件传值

在子组件:中使用 this.$emit(‘getChildInput’,value)函数给父组件传值,

父组件:使用 @getChildInput="getChildInput"来接收传过来的值,其中"getChildInput"为接收的方法,需要自己定义,其中参数为子组件传过来的值,具体用法见代码实现。

子组件

<template>
  <div>
    <div class="child">
      <div>
        <h1>子组件</h1>
      </div>
      <div class="search">
        <a-input-search
          placeholder="传值给父组件"
          enter-button="Emit传值给父组件"
          size="large"
          @search="onSearch"
        />
      </div>
    </div>
  </div>
</template>

<script lang="ts">
import { Component, Vue, Prop,Emit } from "vue-property-decorator";

@Component({
  components: {},
})
export default class Child extends Vue {
    onSearch(value:any) {
      console.log(value);
      this.$emit('getChildInput',value);//通过emit给父组件传值
    }
}
</script>

父组件

<template>
  <div>
    <div class="parent">
      <h1>父组件名字:{{parentName}}</h1>
       <h1>子组件用emit传过来的值:{{childEmitValue}}</h1>
    </div>
     <!-- 通过@getChildInput="getChildInput"获取值,其中@getChildInput为子组件传过来的变量名,
      "getChildInput"为接收值的方法,该方法的参数就是传过来的值
 -->
    <Child
     @getChildInput="getChildInput"
     />

  </div>
</template>

<script lang="ts">
import { Component, Vue } from 'vue-property-decorator';
import Child from '@/components/Child.vue';

@Component({
  components: {
    Child
  },
})
export default class Home extends Vue {
  parentName:string='父组件'
  childEmitValue:any='';
  getChildInput(value:any){
  //获取子组件用emit传过来的值
    this.childEmitValue=value;//赋值在父组件展示
  }
}
</script>

实现效果图
在这里插入图片描述


http://www.niftyadmin.cn/n/855643.html

相关文章

vue ts父组件使用ref调用子组件的方法或获取子组件的值

vue2.0 ts父组件使用ref调用子组件的方法和值 父组件中用ref绑定子组件&#xff0c;然后通过this.$refs.名字就可以获取子组件的值或调用相关方法。 父组件 <template><div><div class"parent"><h1>父组件名字:{{parentName}}</h1>&…

vue+ts:Vue.set在对象和数组中的具体运用

为什么用Vue.set 最近在Vue项目实践中发现&#xff0c;有时候直接改变数组或对象的在值&#xff0c;值改变了&#xff0c;但页面却没有同时修改&#xff0c;通过查看Vue文档才发现&#xff0c;原来Vue不能检测数组或对象的变化&#xff0c;要想改变数组或对象的值时&#xff0…

[antdv: FormModel] model is required for resetFields to work

今天在用Vueant的UI框架进行表单绑定的时候出现报错 Warning: [antdv: FormModel] model is required for resetFields to work.通过不断测试才发现是因为没有在表头使用 :model"xxx"进行数据绑定 <a-form-model :model"form" :rules"rules" …

ant对a-date-picker做时间限制处理(选择下个时间不能早于上个时间)

实现效果: HTML标签模块 <a-row><a-col :span"12">开始时间&#xff1a;<a-date-pickerstyle"width: 100%"change"onChange"valueFormat"YYYY-MM-DD"v-model"startTime"/></a-col><a-col :span&…

antv自定义表单验证方法以及具体实现(身份证,手机,邮箱验证)

实现效果 实现思路 通过antv表单验证api接口得知&#xff0c;如果想自定义表单校验规则需要使用validator字段调用自定义校验方法&#xff0c;自定义方法需要传rule, value, callback这三个参数。知道这些使用方法就可以动手敲代码了&#xff0c;具体实现如下 HTML模块 <…

我会好好表演的

晚上就要比赛了~~ 这个星期&#xff0c;大家都非常非常努力地排练&#xff0c;在大家的心里都觉得这一场比赛很重要&#xff0c;很关键。我也一直在很用功地排练&#xff0c;我一定会在舞台上好好表现的&#xff01; 这场比赛要和来自韩国的SARA一起跳舞&#xff0c;她的舞真的…

vue+ts:全局过滤器和局部过滤器从注册到具体使用的详细过程

在Vue.js中Filter过滤器是什么 在Vue.js中Filter常用于拦截数据&#xff0c;对数据进行进一步处理和展示&#xff0c;分为全局过滤器和局部过滤器&#xff0c;在网页开发中&#xff0c;最常用到的是对时间的处理&#xff0c;比如我们创建个时间&#xff0c;new Date&#xff0…

JS的null和undefined的深度理解与比较

null是一个表示"无"的对象&#xff0c;转为数值时为0&#xff1b;undefined是一个表示"无"的原始值&#xff0c;转为数值时为NaN。 nullundefined//true&#xff0c; 表示相等 &#xff08;值相等&#xff09; nullundefined//false&#xff0c;表示恒等&…