vue-cli学习笔记 02、Vue-router(路由)(二)

简介: vue-cli学习笔记 02、Vue-router(路由)(二)

三、嵌套路由(示例)


当我们有多页面时,就可以使用到嵌套路由。



此时我们在App.vue设置一个router-view,其他单独的页面中再嵌套router-view,就能够实现多页面的效果了。(实际上还是单页面,只不过进行了局部刷新罢了)



App.vue:源头,单页面


<template>
  <div id="nav">
    <router-view />
  </div>
</template>
<script>
export default {
  name: "App",
  components: {},
  methods: {
    linktoHome() {
      this.$router.push("/home");
    },
    linktoAbout() {
      this.$router.push("/About");
    },
  },
};
</script>
<style>
</style>



应用于App.vue的about、home:其中about.vue与home.vue你就可以看成是两个页面


router/index.js:路由配置
import { createRouter, createWebHashHistory } from 'vue-router'
import Home from '../views/home/Home.vue'
import message from '../views/about/components/message.vue'
import banner from '../views/about/components/banner.vue'
import news from '../views/home/components/news.vue'
const routes = [
  {
    path: '/',
    redirect: '/home'
  },
  {
    path: '/home',
    name: 'Home',
    component: Home,
    children: [
      { path: '/home/message', component: message }
    ]
  },
  {
    path: '/about',
    name: 'About',
    component: () => import(/* webpackChunkName: "about" */ '../views/about/About.vue'),
    children: [
      { path: '/about/news', component: news },
      { path: '/about/banner', component: banner }
    ]
  }
]
const router = createRouter({
  history: createWebHashHistory(),
  routes
})
export default router



home目录:


Home.vue
<template>
  welcome home
  <router-view />
</template>
<script>
export default {
  name: "Home",
  components: {},
  mounted() {
    this.$router.push("/home/message");
  },
};
</script>


news.vue:应用于Home.vue


<template>
  <div>
    <ul>
      <li>news1</li>
      <li>news2</li>
      <li>news3</li>
      <li>news4</li>
      <li>news5</li>
    </ul>
  </div>
</template>
<script>
export default {
  name: "news",
};
</script>
<style>
</style>



about目录:


About.vue
<template>
  welcome about
  <el-button type="danger" @click="handleclick">点我一下</el-button>
  <router-view />
</template>
<script>
export default {
  name: "About",
  components: {},
  mounted() {
    this.$router.push({
      path: "/about/news",
      query: {
        name: "changlu",
        age: 18,
      },
    });
    console.log(this.$route);
  },
  methods: {
    handleclick() {
      this.$router.push("/about/banner");
    },
  },
};
</script>
<style>
</style>


应用于About.vue的两个局部组件:


message.vue
<template>
  <div>
    <ul>
      <li>message1</li>
      <li>message2</li>
      <li>message3</li>
      <li>message4</li>
      <li>message5</li>
    </ul>
  </div>
</template>
<script>
export default {
  name: "message",
};
</script>
<style>
</style>



banner.vue


<template>
  <el-carousel indicator-position="outside">
    <el-carousel-item v-for="item in 4" :key="item">
      <h3>{{ item }}</h3>
    </el-carousel-item>
  </el-carousel>
</template>
<script>
export default {
  name: "banner",
};
</script>
<style>
.el-carousel__item h3 {
  color: #475669;
  font-size: 18px;
  opacity: 0.75;
  line-height: 300px;
  margin: 0;
}
.el-carousel__item:nth-child(2n) {
  background-color: #99a9bf;
}
.el-carousel__item:nth-child(2n + 1) {
  background-color: #d3dce6;
}
</style>




此时就能够实现多页面!



四、404页面实现


添加一个404组件:



router/index.js:进行路由配置
{
  path: "/404",
  name: "notFound",
  component: notfound
  },
      //匹配没有配置的路由路径
      {
          path: "/:pathMatch(.*)",
              redirect: '/404'
      }



五、进阶


5.1、导航守卫


导航守卫


全局前置守卫:一般用于全局,每次跳转一个路由都会触发


// 路由守卫
router.beforeEach((to, from, next) => {
  const isLogin = localStorage.isLogin   //一般是否登陆验证信息放置在localstorage中
  // to.name === 'Login'是为了防止拦截死循环!
  if (isLogin || to.name === 'Login') {
    next() // 执行了该函数才会进行跳转
  } else {
    // 跳转至指定login组件
    next({ name: 'Login' })
  }
})



路由独享守卫:针对于某个路由


{
  path: '/',
  name: 'Login',
  component: Login,
  beforeEnter: (to, from, next) => {  //若是能够登陆直接进行跳转主页
    const { isLogin } = localStorage
    isLogin ? next({ name: 'About' }) : next()
  }
},


组件内的守卫:针对于某个组件


全局后置钩子:路由跳转之后


相关文章
|
1天前
|
JavaScript
学习和分享关于 Vue.js 的路由(vue-router)
学习和分享关于 Vue.js 的路由(vue-router)
9 2
|
4天前
|
JavaScript 前端开发 程序员
探索Vue.js宝库:解锁基础知识与实用技能之门(1. 数据绑定与响应式 2. 条件与循环 3. 组件化开发;1. 路由管理与导航 2. 状态管理与Vuex 3. Vue.js的生命周期)
探索Vue.js宝库:解锁基础知识与实用技能之门(1. 数据绑定与响应式 2. 条件与循环 3. 组件化开发;1. 路由管理与导航 2. 状态管理与Vuex 3. Vue.js的生命周期)
9 1
|
4天前
|
JavaScript 前端开发
Vue.js中使用JavaScript实现路由跳转详解
Vue.js中使用JavaScript实现路由跳转详解
6 0
|
4天前
|
JavaScript
Vue搭配ELEMENT组件,路由不能正确跳转bug
Vue搭配ELEMENT组件,路由不能正确跳转bug
Vue搭配ELEMENT组件,路由不能正确跳转bug
|
4天前
|
JavaScript 前端开发 网络架构
Vue如何实现页面跳转路由,实现单页面跳转
Vue如何实现页面跳转路由,实现单页面跳转
|
4天前
|
JavaScript 网络架构
vue3 Elementplus 动态路由菜单不跳转问题
vue3 Elementplus 动态路由菜单不跳转问题
19 1
|
6天前
|
JavaScript API 网络架构
Vue3路由机制router(2)
Vue3路由机制router(2)
12 0
|
6天前
|
安全 定位技术 数据安全/隐私保护
Vue3路由机制router(1)
Vue3路由机制router(1)
16 0
|
6天前
|
JavaScript 前端开发
脚手架vue-cli自定义创建Vue项目,完整详细步骤!
脚手架vue-cli自定义创建Vue项目,完整详细步骤!
|
6天前
|
JavaScript 网络架构
vue路由跳转之【编程式导航与传参】
vue路由跳转之【编程式导航与传参】