vue3使用setup语法糖组件基础传值

**(1):defineProps:**传入要使用的props定义自定义属性,传递过来的值具有响应式,和props一样;

**(2):defineEimts:**传入要自定义的事件,emit实例去传入自定义事件的值,和$emit或context.emit一样;

父组件:

javascript 复制代码
<template>
   <initInput :index="8" @changeInit="changeInit"></initInput>
</template>

<script lang="ts" setup>
import initInput from './computed/initInput.vue';
const changeInit = (e: any) => {
  console.log(e);  // 100
}
</script>

子组件:

javascript 复制代码
<template>
    <div>
        <el-button type="primary" @click="change">点击</el-button>
    </div>
</template>
<script lang="ts" setup>
import { onMounted, getCurrentInstance } from 'vue';
import { useRoute, useRouter } from 'vue-router';
// data数据
let { proxy: this_ }: any = getCurrentInstance();
let route = useRoute();
let router = useRouter();
// mounted
// methods方法
let props = defineProps({
    index: {
        type: Number,
        default: 0
    }
});
let emit = defineEmits(["changeInit"]);
const change = () => {
    console.log(props.index);  // 8
    emit("changeInit", 100);
}
</script>
<style scoped  lang='scss'>
</style>
相关推荐
未来之窗软件服务32 分钟前
未来之窗昭和仙君(六十九)前端收银台行为异常检测—东方仙盟练气
前端·仙盟创梦ide·东方仙盟·昭和仙君
大叔编程奋斗记1 小时前
两个日期间的相隔年月计算
前端·salesforce
上海合宙LuatOS1 小时前
LuatOS核心库API——【io】 io操作(扩展)
java·服务器·前端·网络·单片机·嵌入式硬件·物联网
GISer_Jing3 小时前
Taro多端开发
前端·react.js·taro
未来龙皇小蓝3 小时前
RBAC前端架构-04:设置代理及开发配置
前端·vue.js
祈安_3 小时前
深入理解指针(一)
c语言·前端
SuperEugene3 小时前
对象数组的排序与分组:sort / localeCompare / 自定义 compare
前端·javascript·面试
扶苏10024 小时前
“解构”与“响应”的博弈——深入剖析 Vue 3 的 toRef 与 toRefs
前端·javascript·vue.js
icestone20005 小时前
使用Cursor开发大型项目的技巧
前端·人工智能·ai编程
Channing Lewis5 小时前
zoho crm的子表添加行时,有一个勾选字段,如何让它在details页面新建子表行(点击add row)时默认是勾选的
开发语言·前端·javascript