首页 > 其他分享 >04_Vue Router

04_Vue Router

时间:2024-04-08 19:33:21浏览次数:15  
标签:5173 Vue 04 views content vue import Router path

官网: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文件用来定义路由规则

import { createRouter, createWebHashHistory, createWebHistory } from "vue-router"

//定义路由
const routes = [
    {
        path: "/", // http://localhost:5173
        component: () => import("../views/index.vue")
    },
    {
        path: "/content", // http://localhost:5173/content
        component: () => import("../views/content.vue")
    },
]

const router = createRouter({
    //使用url的#符号之后的部分模拟url路径的变化,因为不会触发页面刷新,所以不需要服务端支持
    //history: createWebHashHistory(),  //哈希模式
    history: createWebHistory(),
    routes }) 

export default router

 

main.js 修改

import { createApp } from 'vue'

//导入Pinia的createPinia方法,用于创建Pinia实例(状态管理库)
import { createPinia } from 'pinia'
//从 pinia-plugin-persistedstate 模块中导入 piniaPluginPersistedstate
import piniaPluginPersistedstate from 'pinia-plugin-persistedstate'
import App from './App.vue'

import router from './router'

const pinia=createPinia();
//将插件添加到 pinia 实例上
pinia.use(piniaPluginPersistedstate)

const app=createApp(App);
app.use(pinia);
app.use(router);
app.mount('#app');

 

app.vue

<script setup>

</script>

<template>
<router-view/>
</template>

<style  scoped>

</style>

 

配置路径别名@

修改路径别名文件:\vuedemo\vite.config.js

import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import path from 'path' //导入 node.js path

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [vue()],
  resolve: {
    alias: { //配置路径别名
      '@': path.resolve(__dirname, 'src')
    }
  }
})

 

 修改index.js路径

//定义路由
const routes = [
    {
        path: "/", // http://localhost:5173
        component: () => import("@/views/index.vue")
    },
    {
        path: "/content", // http://localhost:5173/content
        component: () => import("@/views/content.vue")
    },
]

这里就是把..修改成@

 

路径提示设置

添加\vuedemo\jsconfig.json文件

{
    "compilerOptions": {
      "baseUrl": ".",
      "paths": {
        "@/*": ["src/*"] // 配置 @ 符号指向 src 目录及其子目录
      }
    }
  }

 

路径传递参数

//定义路由
const routes = [
    {
        path: "/", // http://localhost:5173
        component: () => import("@/views/index.vue")
    },
    {
        path: "/content", // http://localhost:5173/content
        component: () => import("@/views/content.vue")
    },
    
    {
        path: "/user/:id", 
        component: () => import("@/views/user.vue")
    },
]

访问路径:

http://localhost:5173/content?name=张三&age=23

http://localhost:5173/user/5

<template>
<h3>Content页面.....</h3>
<br>
Name: {{ $route.query.name }} <br>
Age: {{ $route.query.age }}
</template>
<template>
Id: {{ $route.params.id }} <br>
</template>

 

 

index.vue 转到content.vue

index.vue

<script setup>
 import { useRouter } from 'vue-router';
        const router = useRouter()
        const goTo = ()=> {
            //router.push("/content?name=张三&age=23")
            router.push({ path: '/content', query: { name: '李四', age: 26 } })
        }
</script>

<template>
<h3>Index页面......</h3>
<br>
<!-- 编程式导航 -->
<button @click="goTo()">编程式导航</button>
</template>

<style  scoped>

</style>

content.vue

<script setup>

</script>

<template>
<h3>Content页面.....</h3>
<br>
Name: {{ $route.query.name }} <br>
Age: {{ $route.query.age }}
</template>

<style  scoped>

</style>

 

嵌套路由结合共享组件

添加页面:

\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

import { createRouter, createWebHashHistory, createWebHistory } from "vue-router"

//定义路由
const routes = [
    {
        path: "/", // http://localhost:5173
        alias:["/home","/index"],
        component: () => import("@/views/index.vue")
    },
    {
        path: "/content", // http://localhost:5173/content
        component: () => import("@/views/content.vue")
    },
    
    {
        path: "/user/:id", 
        component: () => import("@/views/user.vue")
    },
    {
        path: "/vip", 
        component: () => import("@/views/vip.vue"),
        children: [ // 子路由
            {
                path: '', // 默认页 http://localhost:5173/vip
                component: import("@/views/vip/default.vue")
            },
            {
                path: 'order', // 会员订单 http://localhost:5173/vip/order
                component: import("@/views/vip/order.vue")
            },
            {
                path: 'info', // 会员资料 http://localhost:5173/vip/info
                component: import("@/views/vip/info.vue")
            }
        ]
    },
    {
        path: "/svip", // http://localhost:5173/svip
        redirect: "/vip" // 重定向
        //redirect: { name: 'history', params: { id: '100', name: 'David' } }
    },
]

const router = createRouter({
    //使用url的#符号之后的部分模拟url路径的变化,因为不会触发页面刷新,所以不需要服务端支持
    //history: createWebHashHistory(), 
    history: createWebHistory(),
    routes
})

export default router

访问:http://localhost:5173/vip/、http://localhost:5173/vip/info、http://localhost:5173/svip/ (重定向)

 

标签:5173,Vue,04,views,content,vue,import,Router,path
From: https://www.cnblogs.com/MingQiu/p/18122381

相关文章

  • 20240408打卡
    第七周第一天第二天第三天第四天第五天第六天第七天所花时间5h代码量(行)469博客量(篇)1知识点了解完成了python大作业,花费两天完成音频处理工具......
  • vuex分了多个模块,利用语法糖调用不同模块里的方法
    //store/modules/a.jsexportdefault{state:{...},getters:{...},mutations:{...},actions:{...}}//store/modules/b.jsexportdefault{state:{...},getters:{...},mutations:{...},actions:{...}}//store/in......
  • Ubuntu 22.04安装在大容量硬盘时默认只启用100G
    背景买了长江致钛的4T硬盘,重新安装Ubuntu22.04后,发现硬盘空间只有100G。本文就是解决这个问题。安装Ubuntu的过程安装到这一步的时候,默认是100G:进入修改界面:修改界面这里显示的是100G:修改成前一步中看到的最大容量1021.996G:在这一步中,可以看到这两行都是1021......
  • 时间戳转换vue
    1.格式化时间 <p>{{formattedTime('1712054698000')}}</p>constformattedTime=(time:any)=>{  constdate=newDate(time)  constyear=date.getFullYear()  constmonth=String(date.getMonth()+1).padStart(2,'0&#......
  • 02_使用Vite搭建Vue3项目
    首先插件添加:LiveServer、Vue-Official、VueVSCodeSnippets、别名路径跳转官网:Vite|下一代的前端工具链(vitejs.dev)1.创建一个文件夹VueApp,运行cmd转到该目录下,执行命令:npmcreatevite@latest2.然后转到vuedemo目录下命令:cdvuedemo,3.执行命令:npminstall。文件夹......
  • Vue2中使用iframe展示文件流(PDF)以及blob类型接口错误展示返回值
    需求使用iframe展示后端接口传输来的文件流(pdf),如果接口返回错误则弹出提示html部分<iframe:src="url"width="100%"/>接口部分//接口封装已忽略,注意:如需接口接收文件流,请在请求中加入responseType:'blob'以及type:"application/json;chartset=UTF-8"function......
  • 前端【VUE】09-vue【Eslint】
    一、ESLint在vscode插件中搜索ESLint,https://marketplace.visualstudio.com/items?itemName=dbaeumer.vscode-eslint 什么是ESLint官方概念:ESLint是可组装的JavaScript和JSX检查工具。通俗理解:一个工具,用来约束团队成员的代码风格。当通过@vue......
  • 算法打卡day37|动态规划篇05| Leetcode1049.最后一块石头的重量II、494.目标和、474.
    算法题Leetcode1049.最后一块石头的重量II题目链接:1049.最后一块石头的重量II 大佬视频讲解:最后一块石头的重量II视频讲解 个人思路和昨天的分割等和子集有些相像,这道题也是尽量让石头分成重量相同的两堆,相撞之后剩下的石头最小,这样就化解成01背包问题了。解法......
  • Vue.nextTick() 使用场景及实现原理
    Vue.nextTick()基本使用作用:等待下一次DOM更新刷新的工具方法。为什么需要用到Vue.nextTick()?当你在Vue中更改响应式状态时,最终的DOM更新并不是同步生效的,而是由Vue将它们缓存在一个队列中,直到下一个“tick”才一起执行。这样是为了确保每个组件无论发生多少......
  • Vue3 · 小白学习全局 API:常规
    全局API:常规本次笔记versionnextTick()defineComponent()defineAsyncComponent()defineCustomElement()1.version暴露当前所使用的Vue版本。类型string示例import{version}from'vue'console.log(version)2.nextTick()等待下一次DOM更新刷新的工具......