vue3 script setup 中使用 component 动态切换导航栏

//父组件
<template>
  <Header @changeNav="changeNav"></Header>
  <component :is="dom[activeNav.type]"></component>
</template>
<script setup >
import Header from "@/components/header/index.vue"; 
import Home from "@/pages/home/index.vue";
import Essay from "@/pages/essay/index.vue";
import Share from "@/pages/share/index.vue";
import About from "@/pages/about_/index.vue";
import SpecialColumn from "@/pages/specialColumn/index.vue";
import { shallowRef, ref, reactive } from "vue";
let activeNav = reactive({
  type: "Home",
});
let dom = shallowRef({
  'Home': Home,
  'Essay': Essay,
  'Share': Share,
  'About': About,
  'SpecialColumn': SpecialColumn,
});
let changeNav = (item) => {
  console.log(item);
  activeNav.type = item;
};
</script>

 //子组件
<template>
  <div class="h-16 lh64 bg-gray-600 border-b-2 border-green-400">
    <div class="w-3/5 ht100 m0auto flex justify-between">
      <div  class=" text-center f30 text-white Sounso w-24 inline-block flex-initial mr-5">Eden
      </div>
      <div class="flex justify-between text-white Sounso text-center w-4/5">
        <div
          class="w-1/5 cursor-pointer"
          @click="change(item)"
          v-for="item in list.navList"
          :key="item.id"
        >
          {{ item.title }}
        </div>
      </div>
    </div>
  </div>
</template>

    
<script setup lang='ts'>
import { reactive,ref} from "vue";
const emit = defineEmits(["changeNav"]);
const list = reactive({
  navList: [
    {
      id: 1,
      value:"Home",
      title: "网站首页",
      icon: "",
    },
    {
      id: 2,
      title: "文章专栏",
      value:"Essay",
      icon: "",
    },
    {
      id: 3,
      title: "资源分享",
      value:"Share",
      icon: "",
    },
    {
      id: 4,
      title: "归档整理",
      value:'SpecialColumn',
      icon: "",
    },
    {
      id: 5,
      title: "关于本站",
      icon: "",
      value:"About"
    },
  ]
});
  let change= (item:any)=>{
  emit('changeNav',item.value)
  }
</script>

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

希尔瓦娜斯~

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值