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>
相关推荐
weixin_4643076312 分钟前
设置程序自启动
前端
小满zs17 分钟前
Next.js第十七章(Script脚本)
前端·next.js
小满zs1 小时前
Next.js第十六章(font字体)
前端·next.js
喝拿铁写前端6 小时前
别再让 AI 直接写页面了:一种更稳的中后台开发方式
前端·人工智能
A向前奔跑7 小时前
前端实现实现视频播放的方案和面试问题
前端·音视频
十一.3668 小时前
131-133 定时器的应用
前端·javascript·html
xhxxx8 小时前
你的 AI 为什么总答非所问?缺的不是智商,是“记忆系统”
前端·langchain·llm
3824278279 小时前
python:输出JSON
前端·python·json
2503_928411569 小时前
12.22 wxml语法
开发语言·前端·javascript