vue3如何实现点击不同的菜单页切换局部页面

不借助vue-router

使用第三方库tinrh实现

介绍:tinrh可以将组件挂载到任意一个出现在页面的节点上

前提:1.元素节点必须配置id:<div id="app"></div>

           2.组件必须配置name属性 defineOptions({ name: 'test' })

           3.元素节点必须已经存在在页面上

使用:

根据文档提示操作:

安装:

$ npm i tinrh

或者

$ yarn add tinrh

在入口文件注册:

import { createApp, nextTick, ref } from 'vue';
import { setFile, register, setLibrars, resolveFn } from 'tinrh';
import 'ant-design-vue/dist/antd.css';
import Antd from 'ant-design-vue';
import App from './App.vue';

const app = createApp(App);
resolveFn({ createApp, nextTick, ref });
const component: any = import.meta.glob('@/example/**/index.vue', {
  eager: true,
});
register(component);
setLibrars([Antd]);
setFile(component);

注册即可即可在项目中使用

如现在需要将Test组件挂载到app节点下

Test组件代码

// 需要挂载的 Test 组件
<template>
  <div>
    <div class="bg">TEST组件</div>
  </div>
</template>
<script setup lang="ts">
import { useAttrs } from 'vue'
// 必须配置组件名称,且唯一!!!
defineOptions({
  name: 'Test',
});

// 获取打开组件时传递的数据
const attrs = useAttrs()

</script>

<style scoped>
.bg {
  width: 200px;
  height: 200px;
  background-color: pink;
}
</style>

在去往App组件

//  app 组件
<template>
  <div id="app"></div>
</template>
<script setup lang="ts">
import { onMounted } from 'vue';
import { R } from 'tinrh';

onMounted(() => {
  // 打开组件Test, 挂载到app节点下
  R('Test', 'app');
});

这时R函数会将Test组件挂载到app节点的下面;

具体使用请查看文档示例

https://tinrh.vercel.app/

  • 8
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
实现菜单页面切换,可以使用Vue.js和Element UI中的el-menu组件。 1. 首先,在Vue.js中定义一个数据对象,用来存储菜单选项和对应的页面路由: ``` data() { return { menuItems: [ { index: 'home', title: '首', route: '/' }, { index: 'about', title: '关于我们', route: '/about' }, { index: 'contact', title: '联系我们', route: '/contact' } ] } } ``` 2. 在模板中,使用el-menu组件来渲染菜单。设置mode属性为horizontal,表示菜单水平排列;设置default-active属性为当前选中的菜单项的index值: ``` <el-menu :default-active="$route.path" mode="horizontal"> <el-menu-item v-for="item in menuItems" :key="item.index" :index="item.route">{{ item.title }}</el-menu-item> </el-menu> ``` 3. 在路由配置中,定义对应的页面组件,并设置路由路径为菜单项的route值: ``` const routes = [ { path: '/', component: Home }, { path: '/about', component: About }, { path: '/contact', component: Contact } ] ``` 4. 最后,在页面组件中,使用Vue.js的路由功能来实现页面切换。例如,在点击菜单项时,使用this.$router.push()方法将路由切换到对应的页面: ``` methods: { handleMenuSelect(index) { this.$router.push(index) } } ``` 完整代码示例: ``` <template> <el-menu :default-active="$route.path" mode="horizontal" @select="handleMenuSelect"> <el-menu-item v-for="item in menuItems" :key="item.index" :index="item.route">{{ item.title }}</el-menu-item> </el-menu> </template> <script> import Home from './Home.vue' import About from './About.vue' import Contact from './Contact.vue' export default { data() { return { menuItems: [ { index: 'home', title: '首', route: '/' }, { index: 'about', title: '关于我们', route: '/about' }, { index: 'contact', title: '联系我们', route: '/contact' } ] } }, methods: { handleMenuSelect(index) { this.$router.push(index) } }, components: { Home, About, Contact } } </script> <style scoped> /* 样式 */ </style> ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值