概述
子组件
<ul class="nav nav-pills">
<router-link to="/" active-class="current" replace>
<li class="nav-item" @mouseover="mouseOver(1)" @mouseleave="mouseLeave()"
@click="clickTab(1)">
<a class="nav-link
mouse"
:class="mouseType==1?'active1':'',activeType==1?'active':'',activeType==1&&mouseType==1?'active2':''"
id="navTab" href="#">首页</a>
</li>
</router-link>
<router-link to="/videoHome" active-class="current" replace>
<li class="nav-item" @mouseover="mouseOver(2)" @mouseleave="mouseLeave"
@click="clickTab(2)">
<a class="nav-link"
:class="mouseType==2?'active1':'',activeType==2?'active':'',activeType==2&&mouseType==2?'active2':''"
id="navTab" href="#">视频直播</a>
</li>
</router-link>
</ul>
defineProps({
activeType: Number,
mouseType: Number
})
//子组件像父组件传值
const emit = defineEmits(["tabMsg"]);
const clickTab = (e) => {
emit("tabMsg", e);
}
//
移入
const mouseOver = (e) => {
emit("tabMsg", {
mouseOver: e
});
}
// 移出
const mouseLeave = (e) => {
emit("tabMsg", {
mouseLeave: ''
});
}
父组件
<ListShowImg :list='list' :showIndex="showIndex"
@onReceiveMsg="onReceiveMsg"></ListShowImg>
// 接受
tabMsg(e) {
console.log('tab切换', e);
if(e.mouseOver){
this.mouseType=e.mouseOver
return
}
if(e.mouseLeave){
this.mouseType=e.mouseLeave
return
}
if(e.tabIndex){
this.tabIndex = e.tabIndex + ''
}
}
完整代码 子组件
<script setup>
import {
ref
} from 'vue'
defineProps({
activeType: Number,
mouseType: Number
})
//子组件像父组件传值
const emit = defineEmits(["tabMsg"]);
const clickTab = (e) => {
emit("tabMsg", e);
}
//
移入
const mouseOver = (e) => {
emit("tabMsg", {
mouseOver: e
});
}
// 移出
const mouseLeave = (e) => {
emit("tabMsg", {
mouseLeave: ''
});
}
const count = ref(1)
</script>
<template>
<div class="row">
<div class="col-sm-9
" style="padding: 0 !important;">
<div class="topNavBorder">
<div class="topNav">
<ul class="nav nav-pills">
<router-link to="/" active-class="current" replace>
<li class="nav-item" @mouseover="mouseOver(1)" @mouseleave="mouseLeave()"
@click="clickTab(1)">
<a class="nav-link
mouse"
:class="mouseType==1?'active1':'',activeType==1?'active':'',activeType==1&&mouseType==1?'active2':''"
id="navTab" href="#">首页</a>
</li>
</router-link>
<router-link to="/videoHome" active-class="current" replace>
<li class="nav-item" @mouseover="mouseOver(2)" @mouseleave="mouseLeave"
@click="clickTab(2)">
<a class="nav-link"
:class="mouseType==2?'active1':'',activeType==2?'active':'',activeType==2&&mouseType==2?'active2':''"
id="navTab" href="#">视频直播</a>
</li>
</router-link>
</ul>
</div>
</div>
</div>
<!--
<div class="col-sm-3
" >123
</div> -->
</div>
</template>
<style>
.alert {
border-radius: 0 !important;
padding: 1rem 0 !important;
}
.alert-light {
border-color: #F0F0F0 !important;
}
.paMa0 {
padding: 0 !important;
/* margin: 0 !important; */
margin-top: 1.6875rem;
}
.paMa0 .alert {
padding: 0 !important;
margin: 0 !important;
}
@media screen and (max-width:500px) {
.topNavBorder {
padding: 0.75rem;
background: #fff;
border-radius: 0.5rem;
}
}
@media screen and (min-width:500px) {
.topNavBorder {
padding: 0.75rem;
margin-right: 0.5rem;
background: #fff;
border-radius: 0.5rem;
}
}
.topNav {
display: flex;
flex-direction: row;
flex-wrap: wrap;
/* border: 1px solid #000;
overflow-x: scroll; */
}
.topNavItem {
padding: 0.375rem 0.5rem;
margin-right: 1.1875rem;
font-size: 1.125rem;
font-family: Source Han Sans CN;
font-weight: 400;
line-height: 3rem;
}
.topNav .active {
background: #78AE30 !important;
border-radius: 0.1875rem !important;
color: #fff !important;
}
.active1 {
color: #78AE30 !important;
}
.active2 {
color: #fff !important;
}
</style>
父组件
<template>
<div class="container">
<div class="container ">
<NavList :activeType="activeType" :mouseType="mouseType"
@tabMsg="tabMsg"></NavList>
</div>
</div>
</div>
</template>
<script>
import $ from 'jquery'
import NavList from '../../components/NavList.vue'
export default {
name: "home",
components: {
NavList,
},
data() {
return {
activeType:1,
showIndex: '1', //展示下标
};
},
methods: {
// 接收
tabMsg(e) {
console.log('tab切换', e);
if(e.mouseOver){
this.mouseType=e.mouseOver
return
}
if(e.mouseLeave){
this.mouseType=e.mouseLeave
return
}
if(e.tabIndex){
this.tabIndex = e.tabIndex + ''
}
}
}
};
</script>
个人笔记爱看不看,不愿看别叭叭,一天天的乱评论烦死了
最后
以上就是诚心睫毛为你收集整理的vue3 setup 绑定事件并向父组件传值的全部内容,希望文章能够帮你解决vue3 setup 绑定事件并向父组件传值所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复