当前位置: 首页 > news >正文

中山精品网站建设流程宁德市房价

中山精品网站建设流程,宁德市房价,网站建设公司名字,wordpress屏蔽右键菜单并提示【Vue3】路由基础 背景简介开发环境开发步骤及源码总结 背景 随着年龄的增长,很多曾经烂熟于心的技术原理已被岁月摩擦得愈发模糊起来,技术出身的人总是很难放下一些执念,遂将这些知识整理成文,以纪念曾经努力学习奋斗的日子。本…

【Vue3】路由基础

  • 背景
  • 简介
  • 开发环境
  • 开发步骤及源码
  • 总结

背景

随着年龄的增长,很多曾经烂熟于心的技术原理已被岁月摩擦得愈发模糊起来,技术出身的人总是很难放下一些执念,遂将这些知识整理成文,以纪念曾经努力学习奋斗的日子。本文内容并非完全原创,大多是参考其他文章资料整理所得,感谢每位技术人的开源精神。

简介

本文介绍 Vue3 中路由的基本写法。

开发环境

分类名称版本
操作系统WindowsWindows 11
IDEVisual Studio Code1.91.1

开发步骤及源码

1> 创建 Vue3 工程,参考:【Vue3】工程创建及目录说明。

2> 删除 src 目录下 assetscomponents 目录。

3> 修改 src 目录下 main.ts

import { createApp } from 'vue'
import App from './App.vue'createApp(App).mount('#app')

4> 创建三个页面组件,注意与功能组件不同,不放在 src/components 目录下,页面组件一般放在 pagesviews 目录下。

  • Dashboard.vue

    <template><div class="dashboard">这是仪表盘页面</div>
    </template><script setup lang="ts">
    </script><style scoped lang="scss">
    </style>
    
  • System.vue

    <template><div class="system">这是系统管理页面</div>
    </template><script setup lang="ts">
    </script><style scoped lang="scss">
    </style>
    
  • About.vue

    <template><div class="about">这是关于页面</div>
    </template><script setup lang="ts">
    </script><style scoped lang="scss">
    </style>
    

5> 执行 npm i vue-router 命令安装路由组件。

6> 在 src 下创建 router 目录,并在其中创建 index.ts 文件,此文件作用是创建并暴露路由器。

import { createRouter, createWebHistory } from 'vue-router'
import Dashboard from '@/pages/Dashboard.vue'
import System from '@/pages/System.vue'
import About from '@/pages/About.vue'const router = createRouter({// 路由器工作模式history: createWebHistory(),routes: [{path: '/dashboard',component: Dashboard},{path: '/system',component: System},{path: '/about',component: About}]
})export default router

7> 修改 main.ts 引入并使用路由器。

import { createApp } from 'vue'
import App from './App.vue'
import router from './router'createApp(App).use(router).mount('#app')

8> 修改根组件 App.vue,调用路由器实现跳转功能。

<template><h1 class="title">Vue3路由</h1><hr><div class="route"><div class="menu"><div class="menu-item"><RouterLink to="/dashboard" active-class="active">仪表盘</RouterLink></div><div class="menu-item"><RouterLink to="/system" active-class="active">系统管理</RouterLink></div><div class="menu-item"><RouterLink to="/about" active-class="active">关于</RouterLink></div></div><div class="content"><RouterView /></div></div>
</template><script setup lang="ts">
import { RouterLink, RouterView } from 'vue-router'
</script><style scoped lang="scss">
.title {text-align: center;
}
.route {display: flex;justify-content: center;.menu {width: 200px;height: 500px;background-color: #F1F2F3;border-radius: 6px;.menu-item {height: 40px;line-height: 40px;text-align: center;margin: 5px;border-radius: 3px;text-decoration: none;}.menu-item:hover {background-color: white;cursor: pointer}a {text-decoration: none;}.active {color: #00AEEC;}}.content {width: 600px;height: 500px;margin-left: 10px;border: 1px solid #F1F2F3;}
}
</style>

9> 执行命令 npm run dev 启动应用,浏览器访问:http://localhost:5173/,点击左侧菜单观察页面变化。
在这里插入图片描述

总结

关键步骤:

  1. 安装路由组件:npm i vue-router
  2. 创建并暴露路由器:src/router/index.ts,包括:
    • 引入 createRouter 用于创建路由器;
    • 通过 createRouter 参数对象的 history 属性配置路由器工作模式,路由器工作模式有两类:createWebHistory()createWebHashHistory(),本文使用的是 createWebHistory(),两类工作模式间的差异将在其他文章中说明;
    • 通过 createRouter 参数对象的 routes 属性配置路由,每个路由由一个路径 path 和一个组件 component 构成;
    • 暴露路由 export default router
  3. 引入路由器:src/main.ts,作用是:
    • 全局注册 RouterLinkRouterView 组件;
    • 添加全局 $router$route 属性;
    • 启用 useRouter()useRoute() 组合式函数;
    • 触发路由器解析初始路由。
  4. 使用 Vue Router 提供的组件实现路由功能:
    • RouterLink:代替常规的 <a> 标签创建链接,使得能够在不重新加载页面的情况下改变 URL,处理 URL 的生成、编码和其他功能;
    • RouterView:渲染当前 URL 路径对应的页面组件。
http://www.15wanjia.com/news/42891.html

相关文章:

  • js怎么做网站建个人网站的详细步骤
  • 网页设计基础教学网站性能优化方法
  • 网络推广的优点app排名优化
  • 南宁市网络推广公司哪家好优化设计数学
  • 免费b2b网站大全黄页88北京网络推广外包公司排行
  • 赣州公司做网站第三方平台推广
  • 江夏区做网站最新新闻热点事件2024
  • 小程序骗局开发报价公司seo排名优化
  • 网站推广的意义和方法关键词挖掘站网
  • 网站建设维护培训网站建设的重要性
  • 临汾市网站建设营销策划思路
  • 电脑建立网站郑州模板建站代理
  • 凡科网站做的好不好baiduseoguide
  • 有模版之后怎么做网站seo 培训教程
  • 做英文网站用什么源码查淘宝关键词排名软件
  • 没有营业执照怎么样做百度企业网站百度推广话术全流程
  • 做网站上的在线支付怎么做友情链接平台网站
  • seo网站分析报告广东云浮疫情最新情况
  • 定远建设局官方网站西安网站seo哪家公司好
  • 新房装修北京网站优化托管
  • 深圳网站建设 东莞网站建设企业网站seo案例
  • 专门做护肤品的网站是搜狗推广开户
  • 苏州注册公司一站式如何创建一个自己的网站
  • 企业网站seo推广技巧石家庄网站seo外包
  • 最优网络做网站平台营销
  • 金融投资网站开发su搜索引擎优化
  • 国外推广国内网站免费推广工具有哪些
  • 国家pccm建设申报网站台州百度关键词排名
  • hao123网址之家搜索引擎关键词怎么优化
  • 做网站页面过大好短链接生成网址