翼度科技»论坛 编程开发 .net 查看内容

04_Vue Router

3

主题

3

帖子

9

积分

新手上路

Rank: 1

积分
9
官网:Vue Router | Vue.js 的官方路由 (vuejs.org)
安装命令:npm install vue-router@4
1.添加两个页面\vuedemo\src\views\index.vue、\vuedemo\src\views\content.vue
2.添加\vuedemo\src\router\index.js文件用来定义路由规则
  1. import { createRouter, createWebHashHistory, createWebHistory } from "vue-router"
  2. //定义路由
  3. const routes = [
  4.     {
  5.         path: "/", // http://localhost:5173
  6.         component: () => import("../views/index.vue")
  7.     },
  8.     {
  9.         path: "/content", // http://localhost:5173/content
  10.         component: () => import("../views/content.vue")
  11.     },
  12. ]
  13. const router = createRouter({
  14.     //使用url的#符号之后的部分模拟url路径的变化,因为不会触发页面刷新,所以不需要服务端支持
  15.     //history: createWebHashHistory(),  //哈希模式
  16.     history: createWebHistory(),
  17.     routes })
  18. export default router
复制代码
 
main.js 修改
  1. import { createApp } from 'vue'
  2. //导入Pinia的createPinia方法,用于创建Pinia实例(状态管理库)
  3. import { createPinia } from 'pinia'
  4. //从 pinia-plugin-persistedstate 模块中导入 piniaPluginPersistedstate
  5. import piniaPluginPersistedstate from 'pinia-plugin-persistedstate'
  6. import App from './App.vue'<strong>
  7. import router from './router'</strong>
  8. const pinia=createPinia();
  9. //将插件添加到 pinia 实例上
  10. pinia.use(piniaPluginPersistedstate)
  11. const app=createApp(App);
  12. app.use(pinia);
  13. <strong>app.use(router);</strong>
  14. app.mount('#app');
复制代码
 
app.vue
  1. [/code] 
  2. [b]配置路径别名@[/b]
  3. 修改路径别名文件:\vuedemo\vite.config.js
  4. [code]import { defineConfig } from 'vite'
  5. import vue from '@vitejs/plugin-vue'<strong>
  6. import path from 'path' </strong>//导入 node.js path
  7. // https://vitejs.dev/config/
  8. export default defineConfig({
  9.   plugins: [vue()],
  10.   <strong>resolve: {
  11.     alias: { </strong><strong>//配置路径别名
  12.       '@': path.resolve(__dirname, 'src'</strong><strong>)
  13.     }
  14.   }</strong>
  15. })
复制代码
 
 修改index.js路径
  1. //定义路由
  2. const routes = [
  3.     {
  4.         path: "/", // http://localhost:5173
  5.         component: () => import("@/views/index.vue")
  6.     },
  7.     {
  8.         path: "/content", // http://localhost:5173/content
  9.         component: () => import("@/views/content.vue")
  10.     },
  11. ]
复制代码
这里就是把..修改成@
 
路径提示设置
添加\vuedemo\jsconfig.json文件
  1. {
  2.     "compilerOptions": {
  3.       "baseUrl": ".",
  4.       "paths": {
  5.         "@/*": ["src/*"] // 配置 @ 符号指向 src 目录及其子目录
  6.       }
  7.     }
  8.   }
复制代码
 
路径传递参数
  1. //定义路由
  2. const routes = [
  3.     {
  4.         path: "/", // http://localhost:5173
  5.         component: () => import("@/views/index.vue")
  6.     },
  7.     {
  8.         path: "/content", // http://localhost:5173/content
  9.         component: () => import("@/views/content.vue")
  10.     },
  11.    
  12.     {
  13.         path: "/user/:id",
  14.         component: () => import("@/views/user.vue")
  15.     },
  16. ]
复制代码
访问路径:
http://localhost:5173/content?name=张三&age=23
http://localhost:5173/user/5
  1. <template>
  2. <h3>Content页面.....</h3>
  3. <br>
  4. Name: {{ $route.query.name }} <br>
  5. Age: {{ $route.query.age }}
  6. </template>
复制代码
  1. <template>
  2. Id: {{ $route.params.id }} <br>
  3. </template>
复制代码
 
 
index.vue 转到content.vue
index.vue
  1. <template>
  2. <h3>Index页面......</h3>
  3. <br>
  4. <button <strong>@click="goTo()"</strong>>编程式导航</button>
  5. </template>
复制代码
content.vue
  1. <template>
  2. <h3>Content页面.....</h3>
  3. <br>
  4. Name: {{ $route.query.name }} <br>
  5. Age: {{ $route.query.age }}
  6. </template>
复制代码
 
嵌套路由结合共享组件
添加页面:
\vuedemo\src\views\vip.vue
\vuedemo\src\views\vip\default.vue
\vuedemo\src\views\vip\info.vue
\vuedemo\src\views\vip\order.vue
\vuedemo\src\views\svip.vue
修改index.js
  1. import { createRouter, createWebHashHistory, createWebHistory } from "vue-router"
  2. //定义路由
  3. const routes = [
  4.     {
  5.         path: "/", // http://localhost:5173
  6.         alias:["/home","/index"],
  7.         component: () => import("@/views/index.vue")
  8.     },
  9.     {
  10.         path: "/content", // http://localhost:5173/content
  11.         component: () => import("@/views/content.vue")
  12.     },
  13.    
  14.     {
  15.         path: "/user/:id",
  16.         component: () => import("@/views/user.vue")
  17.     },
  18.     {
  19.         path: "/vip",
  20.         component: () => import("@/views/vip.vue"),
  21.         children: [ // 子路由
  22.             {
  23.                 path: '', // 默认页 http://localhost:5173/vip
  24.                 component: import("@/views/vip/default.vue")
  25.             },
  26.             {
  27.                 path: 'order', // 会员订单 http://localhost:5173/vip/order
  28.                 component: import("@/views/vip/order.vue")
  29.             },
  30.             {
  31.                 path: 'info', // 会员资料 http://localhost:5173/vip/info
  32.                 component: import("@/views/vip/info.vue")
  33.             }
  34.         ]
  35.     },
  36.     {
  37.         path: "/svip", // http://localhost:5173/svip
  38.         redirect: "/vip" // 重定向
  39.         //redirect: { name: 'history', params: { id: '100', name: 'David' } }
  40.     },
  41. ]
  42. const router = createRouter({
  43.     //使用url的#符号之后的部分模拟url路径的变化,因为不会触发页面刷新,所以不需要服务端支持
  44.     //history: createWebHashHistory(),
  45.     history: createWebHistory(),
  46.     routes
  47. })
  48. export default router
复制代码
访问:http://localhost:5173/vip/、http://localhost:5173/vip/info、http://localhost:5173/svip/ (重定向)
 

来源:https://www.cnblogs.com/MingQiu/p/18122381
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作!

举报 回复 使用道具