vue不同页面切换的方式(Vue动态组件)

news/2024/10/18 3:24:07/

v-if实现

<!--Calender.vue-->
<template><a-calendar v-model:value="value" @panelChange="onPanelChange" /></template>
<script setup>javascript">
import { ref } from 'vue';
const value = ref();
const onPanelChange = (value, mode) => {console.log(value, mode);
};
</script>
<style>
</style>
<!--Table.vue-->
<script setup>javascript">
import { defineComponent, ref } from 'vue'
defineProps({msg: String,
})
const count = ref(0)const columns = [{name: 'Name',dataIndex: 'name',key: 'name',},{title: 'Age',dataIndex: 'age',key: 'age',},{title: 'Address',dataIndex: 'address',key: 'address',},{title: 'Tags',key: 'tags',dataIndex: 'tags',},{title: 'Action',key: 'action',},
];
const data = [{key: '1',name: 'John Brown',age: 32,address: 'New York No. 1 Lake Park',tags: ['nice', 'developer'],},{key: '2',name: 'Jim Green',age: 42,address: 'London No. 1 Lake Park',tags: ['loser'],},{key: '3',name: 'Joe Black',age: 32,address: 'Sidney No. 1 Lake Park',tags: ['cool', 'teacher'],},
];
</script><template><a-table :columns="columns" :data-source="data"><template #headerCell="{ column }"><template v-if="column.key === 'name'"><span><smile-outlined />Name</span></template></template><template #bodyCell="{ column, record }"><template v-if="column.key === 'name'"><a>{{ record.name }}</a></template><template v-else-if="column.key === 'tags'"><span><a-tagv-for="tag in record.tags":key="tag":color="tag === 'loser' ? 'volcano' : tag.length > 5 ? 'geekblue' : 'green'">{{ tag.toUpperCase() }}</a-tag></span></template><template v-else-if="column.key === 'action'"><span><a>Invite 一 {{ record.name }}</a><a-divider type="vertical" /><a>Delete</a><a-divider type="vertical" /><a class="ant-dropdown-link">More actions<down-outlined /></a></span></template></template></a-table>
</template><style scoped>
</style>
<!--index.vue--><script setup>javascript">
import { ref } from 'vue'
import Table from './components/Table.vue'
import Calender from './components/Calender.vue'let table = ref(true)
let calender = ref(false)const changeTa = ()=>{table.value = truecalender.value = false
}const changeCa = ()=>{calender.value = truetable.value = false
}
</script><template><a-button type="primary" v-if="table" @click="changeCa">日历视图</a-button><a-button type="primary" v-if="calender" @click="changeTa">表格视图</a-button><Table v-if="table"></Table><Calender v-if="calender"></Calender>
</template><style scoped>
</style>

效果:

在这里插入图片描述

在路由地址未变化的情况下试下组件的切换。

Vue 的 元素实现

子组件内容不变,父组件实现方式改变,如下:

<!--index.vue-->
<script setup>javascript">
import { ref } from 'vue'
import Table from './components/Table.vue'
import Calender from './components/Calender.vue'
let tableView = ref('Table')
let tableName = ref('日历视图')const changeTa = ()=>{tableView.value = 'Table'
}const tabs = {Table,Calender,
}const changeView = ()=>{if (tableView.value === 'Table'){tableView.value = 'Calender'tableName.value = '表格视图'}else{tableView.value = 'Table'tableName.value = '日历视图'}}
</script><template><a-button type="primary" @click="changeView">{{ tableName }}</a-button> <component :is="tabs[tableView]"></component>
</template><style scoped>
</style>

在这里插入图片描述

这种方式是组件的动态渲染,接下来是路由实现组件的切换。

<RouterLink><RouterView>路由实现

路由必须是全局的在整个路由生命周期都可以使用,并且路由需要作为插件注册到vue实例中。

  • 定义路由规则
import { createMemoryHistory, createRouter } from 'vue-router'
import Table from '../components/Table.vue'
import Calender from '../components/Calender.vue'
const routes = [
{ path: '/calender', component: Table },
{ path: '/table', component: Calender },
]const router = createRouter({
history: createMemoryHistory(),
routes,
})export {router,
}
  • main.js注册路由实例
import { createApp } from 'vue'
import './style.css'
import App from './App.vue'import Antd from 'ant-design-vue';
import 'ant-design-vue/dist/reset.css';// 路由
import {router } from './router/router.js'
const app = createApp(App)app.use(router)app.use(Antd).mount('#app')
  • 路由渲染组件
<template><RouterLink to="/calender">日历视图</RouterLink><br><RouterLink to="/table">表格视图</RouterLink><RouterView />
</template>

效果
在这里插入图片描述

这种标签式的路由渲染组件路由地址也是不变的,相当于动态组件。

编程式渲染组件

仍然需要定义路由规则和上一节一样。

<!--index.vue--><script setup>javascript">
import { ref } from 'vue'
import { useRouter,useRoute } from 'vue-router'let router = useRouter()const changeTable = function() {router.push({path: '/table'})
}const changeCalender = function() {router.push({path: '/calender'})
}</script><template><a-button type="primary" @click="changeCalender">日历视图</a-button><br><a-button type="primary" @click="changeTable">表格视图</a-button><RouterView />
</template><style scoped>
</style>

效果:

在这里插入图片描述


http://www.ppmy.cn/news/1466257.html

相关文章

【DrissionPage爬虫库 1】两种模式分别爬取Gitee开源项目

文章目录 DrissionPage爬虫库简介1. 浏览器操控模式&#xff08;类似于游戏中的后台模拟鼠标键盘&#xff09;2. 数据包收发模式&#xff08;类似于游戏中的协议封包&#xff09; 实战中学习需求&#xff1a;爬取Gitee开源项目的标题与描述解决方案1&#xff1a;用数据包方式获…

Zookeeper 最新稳定版本 3.8.4 服务安装与原生 C 静态库编译

依赖 jdk Zookeeper 服务由 Java 语言开发&#xff0c;需要 jdk 环境&#xff0c;请确保进行下面步骤之前先安装 jdk。 GNU/Linux 系统可以通过包管理工具直接安装 jdk 环境&#xff0c;例如 ubantu 可以直接通过以下命令安装&#xff1a; sudo apt update sudo apt instal…

请说出vue.cli项目中src目录每个文件夹和文件的用法

在Vue CLI项目中&#xff0c;src目录是存放项目源码及需要引用的资源文件的主要位置。以下是src目录下常见文件夹和文件的用法&#xff1a; components 用途&#xff1a;存放可重用的Vue组件。这些组件通常用于在多个页面或布局中共享UI和功能。特点&#xff1a;组件应该是模块…

二级指针简单介绍

我们之前学习的&#xff1a;变量的地址是存入指针变量中的&#xff0c;然而指针变量也是变量&#xff0c;是变量就有地址&#xff0c;那么指针变量的地址存放在哪里 &#xff1f; 这也就是二级指针 #include<stdio.h> int main() {int a10;int*p&a;int**pp&p;re…

上位机图像处理和嵌入式模块部署(f407 mcu中tf卡读写和fatfs挂载)

【 声明&#xff1a;版权所有&#xff0c;欢迎转载&#xff0c;请勿用于商业用途。 联系信箱&#xff1a;feixiaoxing 163.com】 很早之前&#xff0c;个人对tf卡并不是很重视&#xff0c;觉得它就是一个存储工具而已。后来在移植v3s芯片的时候&#xff0c;才发现很多的soc其实…

Web安全:软件开发的安全问题与解决方案

「作者简介」&#xff1a;2022年北京冬奥会网络安全中国代表队&#xff0c;CSDN Top100&#xff0c;就职奇安信多年&#xff0c;以实战工作为基础对安全知识体系进行总结与归纳&#xff0c;著作适用于快速入门的 《网络安全自学教程》&#xff0c;内容涵盖系统安全、信息收集等…

讲解如何使用RAG(检索增强生成)和LLM(大语言模型)来构建一个法律咨询网站。

一、准备工作 1. 注册OpenAI API 首先,注册OpenAI并获取API密钥。 2. 环境配置 安装必要的Python库: pip install openai faiss-cpu sentence-transformers flask二、设计系统架构 整个系统将包括以下几个部分: 前端:用户输入问题和上传文件的界面。后端:处理用户请…

CentOS开启ftp并使用filezilla连接

1. 安装vsftpd sudo yum install vsftpd -y 2. 启动ftp服务 service vsftpd start 3. 加入开机启动 chkconfig vsftpd on 4. 开启端口 sudo firewall-cmd --zonepublic --add-port21/tcp --permanent 5. 重启防火墙 sudo firewall-cmd --reload 6. 查询有哪些端口是开…