vue-router實(shí)現(xiàn)簡單vue多頁切換、嵌套路由、路由跳轉(zhuǎn)的步驟和報(bào)錯(cuò)
官方說明文檔:
vue3選擇V4.x,vue2選擇V3.x
一、安裝vue-router
npm i vue-router@3.6.5 //npm //or yarn add vue-router@3.6.5 //yarn
查看項(xiàng)目依賴中是否有vue-router
二、引入vue-router實(shí)現(xiàn)路由切換
在src文件夾中新建router文件夾,新建index.js文件等等(main.vue是嵌套路由,后期講)
Home.vue示例
<template> <h1>Home</h1> </template> <script> export default { data() { return {} }, } </script>
User.vue示例
<template> <h1>Users</h1> </template> <script> export default { data() { return {} }, } </script>
在index.js文件中寫以下代碼:
import Vue from 'vue' import VueRouter from 'vue-router' import Home from '../views/Home.vue' import User from '../views/User.vue' import Main from '../views/Main.vue' Vue.use(VueRouter) //1、創(chuàng)建路由組件
將路由與組件進(jìn)行映射
const routes = [ { path: '/home', component: Home }, { path: '/user', component: User }, ]
創(chuàng)建 router 實(shí)例,然后傳 `routes` 配置,你還可以傳別的配置參數(shù), 不過先這么簡單著吧。
const router = new VueRouter({ routes, // (縮寫) 相當(dāng)于 routes: routes }) export default router
在app.vue中寫以下代碼:
<template> <div id="app"> <!-- 路由出口 --> <!-- 路由匹配到的組件將渲染在這里 --> <router-view></router-view> </div> </template> <script> export default { name: 'App', } </script>
三、嵌套路由
編寫router|index.js,添加引入、映射
import Vue from 'vue' import VueRouter from 'vue-router' import Home from '../views/Home.vue' import User from '../views/User.vue' import Main from '../views/Main.vue' Vue.use(VueRouter) //1、創(chuàng)建路由組件 //2、將路由與組件進(jìn)行映射 const routes = [ //主路由 { path: '/', component: Main, children: [ //子路由 { path: '/home', component: Home }, { path: '/user', component: User }, ], }, { path: '/home', component: Home }, { path: '/user', component: User }, ] // 3. 創(chuàng)建 router 實(shí)例,然后傳 `routes` 配置 // 你還可以傳別的配置參數(shù), 不過先這么簡單著吧。 const router = new VueRouter({ routes, // (縮寫) 相當(dāng)于 routes: routes }) export default router
編寫側(cè)邊欄樣式
編寫Main.vue文件
<template> <div> //頁面布局 <el-container> <el-aside width="200px"> <common-aside /> </el-aside> <el-container> <el-header>Header</el-header> <el-main> <!-- 路由出口 --> <!-- 路由匹配到的組件將渲染在這里 --> <router-view></router-view> </el-main> </el-container> </el-container> </div> </template> <script> import CommonAside from '../components/CommonAside.vue'// 引入側(cè)邊欄組件(main.vue的樣式) export default { data() { return {} }, //引入組件 components: { CommonAside, }, } </script>
四、路由跳轉(zhuǎn)
在菜單標(biāo)簽中添加click事件:
在script部分中的methods添加clickMenu方法
在views文件夾中新建相關(guān)菜單文件,如第二節(jié)所示。回到router|index.js文件,添加引入、映射
可以對首頁進(jìn)行重定向,當(dāng)點(diǎn)擊首頁時(shí),瀏覽器地址為“/”,但是首頁是“/home”,重定向操作如下:
五、相關(guān)報(bào)錯(cuò)
eslint報(bào)錯(cuò)
在vue.config.js中編寫如下代碼關(guān)閉eslint校驗(yàn):
const { defineConfig } = require('@vue/cli-service') module.exports = defineConfig({ transpileDependencies: true, lintOnSave: false, //關(guān)閉eslint校驗(yàn) })
總結(jié)
到此這篇關(guān)于vue-router實(shí)現(xiàn)簡單vue多頁切換、嵌套路由、路由跳轉(zhuǎn)的步驟和報(bào)錯(cuò)的文章就介紹到這了,更多相關(guān)vue-router多頁切換、嵌套路由、路由跳轉(zhuǎn)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue-router項(xiàng)目實(shí)戰(zhàn)總結(jié)篇
vue-router 是 Vue.js 官方的路由庫.這篇文章主要介紹了vue-router項(xiàng)目實(shí)戰(zhàn)總結(jié),需要的朋友可以參考下2018-02-02vue靜態(tài)配置文件不進(jìn)行編譯的處理過程(在public中引入js)
這篇文章主要介紹了vue靜態(tài)配置文件不進(jìn)行編譯的處理過程(在public中引入js),具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-03-03在vue框架下使用指令vue add element安裝element報(bào)錯(cuò)問題
這篇文章主要介紹了在vue框架下使用指令vue add element安裝element報(bào)錯(cuò)問題,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-10-10Vue2.0表單校驗(yàn)組件vee-validate的使用詳解
本篇文章主要介紹了Vue2.0表單校驗(yàn)組件vee-validate的使用詳解,詳細(xì)的介紹了vee-validate使用教程。具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-05-05