开发公司Vue3的vue-router路由详解

开发公司这篇文章是接着【】开发公司的内容做的开发,开发公司有基础的可以跳过 【】,开发公司直接看以下的内容。

的vue-router路由详解:

开发公司首先安装路由依赖模块:

npm install vue-router@4

开发公司所需代码文件如下图:
图1

 

开发公司所需要的主要文件:index.html、index.js、App.vue


index.html:模板页面,开发公司项目中的代码编译之后都是放入到模板页面中id为app的元素这种。
代码如下:

  1. <!DOCTYPE html>
  2. <html>
  3.   <head>
  4.     <meta charset="utf-8">
  5.     <meta name="viewport" content="width=device-width,initial-scale=1.0">
  6.     <title>Vue</title>
  7.   </head>
  8.   <body>
  9.     <div id="app"></div>
  10.   </body>
  11. </html>

index.js:创建一个vue应用,将路由放入应用之中,并挂载到模板页面id为app的元素上。
代码如下:

  1. import { createApp } from 'vue'
  2. import { createRouter,createWebHashHistory } from 'vue-router'
  3. import App from './App.vue'
  4. // 1. 定义路由组件:这里直接用的对象数据,也可以导入其他组件。
  5. const Main = { render(){ return '月影WEB 欢迎大家来学习各种技术知识!'} }
  6. const Lists = { render(){ return '月影WEB-列表页面'} }
  7. const Details = { render(){ return '月影WEB-详情页面'} }
  8. // 2. 定义一些路由:每个路由都需要映射到一个组件。
  9. const routes = [
  10.     { path: '/', component: Main },
  11.     { path: '/lists', component: Lists },
  12.     { path: '/details', component: Details },
  13. ]
  14. // 3. 创建路由实例并传递 `routes` 配置。
  15. const router = createRouter({
  16.     // 内部提供了 history 模式的实现。为了简单起见,我们在这里使用 hash 模式。
  17.     history: createWebHashHistory(),
  18.     routes, // `routes: routes` 的缩写
  19. })
  20. // 4.创建一个vue应用,将App组件和定义的路由放入到vue应用,并挂载到模板页面id为app的元素上。
  21. createApp(App).use(router).mount('#app')

1. 定义路由组件:这里直接用的对象数据,也可以导入其他组件。
const Main = { render(){ return '月影WEB 欢迎大家来学习各种技术知识!'} }
const Lists = { render(){ return '月影WEB-列表页面'} }
const Details = { render(){ return '月影WEB-详情页面'} }

注意:Main、Lists 、Details 定义了三个路由组件,return后面就是每个路由组件展示的UI。

 

2. 定义一些路由:每个路由都需要映射到一个组件。
const routes = [
    { path: '/', component: Main },
    { path: '/lists', component: Lists },
    { path: '/details', component: Details },
]

注意:path是路由路径,也是地址栏会显示的路径,component是放路由组件的,每个路由路径都可以进行组件映射。

 

3. 创建路由实例并传递 `routes` 配置。
const router = createRouter({
    // 内部提供了 history 模式的实现。为了简单起见,我们在这里使用 hash 模式。
    history: createWebHashHistory(),
    routes, // `routes: routes` 的缩写
})

注意:这里只做了简单的配置,history是路由的模式,routes放定义的路由,createRouter创建路由实例。

 

4.创建一个vue应用,将App组件和定义的路由放入到vue应用,并挂载到模板页面id为app的元素上。
createApp(App).use(router).mount('#app')

 

App.vue:用来展示不同的路由页面UI。
代码如下:

  1. <template>
  2.     <router-view></router-view>
  3. </template>
  4. <script>
  5. export default {
  6. }
  7. </script>

注意:<router-view>是用来展示路由对应的组件UI的。
 

启动服务的效果如下:


{ path: '/', component: Main }

 

{ path: '/lists', component: Lists }


{ path: '/details', component: Details }

 

 

关注公众号(月影WEB),了解更多的前后端知识;
欢迎大家关注互相交流学习;

 

网站建设定制开发 软件系统开发定制 定制软件开发 软件开发定制 定制app开发 app开发定制 app开发定制公司 电商商城定制开发 定制小程序开发 定制开发小程序 客户管理系统开发定制 定制网站 定制开发 crm开发定制 开发公司 小程序开发定制 定制软件 收款定制开发 企业网站定制开发 定制化开发 android系统定制开发 定制小程序开发费用 定制设计 专注app软件定制开发 软件开发定制定制 知名网站建设定制 软件定制开发供应商 应用系统定制开发 软件系统定制开发 企业管理系统定制开发 系统定制开发