欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

vue-router實(shí)現(xiàn)簡單vue多頁切換、嵌套路由、路由跳轉(zhuǎn)的步驟和報(bào)錯(cuò)

 更新時(shí)間:2024年05月11日 09:58:51   作者:June_YYang  
最近學(xué)習(xí)到VUE路由這塊,發(fā)現(xiàn)這塊知識點(diǎn)有點(diǎn)多,好容易混亂,這篇文章主要介紹了vue-router實(shí)現(xiàn)簡單vue多頁切換、嵌套路由、路由跳轉(zhuǎn)的步驟和報(bào)錯(cuò)的相關(guān)資料,需要的朋友可以參考下

官方說明文檔:

介紹 | Vue Router (vuejs.org)

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)文章

最新評論