vue-router路由器的基础原理案例,实现页面无刷新切换

安装命令     cnpm i vue-router

安装完毕后:进行配置路由,在src目录下创建router/index.js文件,在index.js文件中进行配置处理:

// 该文件专门用于创建整个应用路由器
import VueRouter from 'vue-router'
// 引入组件
import st from '../components/HelloWorld'
import stc from '../components/Student'
// 创建一个路由器并且需要暴露
export default  new VueRouter({
    routes:[
        {
            path:'/student',
            component:st
        },
        {
            path:'/class',
            component:stc
        }
    ]
})

 两个组件:HelloWorld.vue和 Student.vue组件自行创建

在我的main.js入口文件中进行配置这个vue-router 插件以及引入刚写好的index.js路由设置文件

// 该文件是整个项目的入口文件

// 引入vue
import Vue from 'vue'
// 引入app组件,是所有组件的父组件,
import App from './App.vue'
// 关闭vue的生产提示
// 引入vue-router插件
import VueRouter from 'vue-router'
// 引入路由
import router from './router/index'
Vue.config.productionTip = false
// 使用vue-router插件
Vue.use(VueRouter)

// 创建vue实例对象,
new Vue({
  // 将app组件放到容器中
  render: h => h(App),
  router:router
}).$mount('#app')

在我的相应的组件中使用router-link to 以及router-view进行操作

<template>
  <div id="app">
      <ul>
          <li>
              <router-link to="/student" active-class="active">学生管理</router-link>
          </li>
          <li>
              <router-link to="/class" active-class="active">课程管理</router-link>
          </li>
      </ul>
      <div id="content">
           <!--指定组件的呈现位置-->
           <router-view></router-view>
      </div>
  </div>
</template>

<script>
import HelloWorld from './components/HelloWorld'
import Student from './components/Student'
export default {
  name: 'App',
  components: {
      HelloWorld,
      Student
  }
}
</script>



<style scoped>
*{
  padding: 0px;
  margin: 0px;
}

ul{
   
    width: 16%;
    float: left;
    text-align:center;
    list-style-type: none;
}
li{
  width: 100%;
  background-color:burlywood;

}
a{
  color:white;
}
#content{
  float: left;
  width: 84%;
  padding: 10px 0px;
  background-color: burlywood;
  text-align: center;
  color: white;
}
.active{
   background-color:blue;
   width: 100%;
}
</style>

最终效果:

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
vue-router实现原理是利用浏览器提供的接口window.history和window.location.hash来实现路由功能。具体来说,vue-router通过配置mode属性来选择使用哪个接口实现路由功能。mode属性有两个选项:hash和history。当mode为hash时,vue-router使用window.location.hash来监听URL的变化,并根据URL的hash值来匹配对应的组件。当mode为history时,vue-router使用HTML5的history API来监听URL的变化,并根据URL的路径来匹配对应的组件。通过这种方式,vue-router能够在不刷新页面的情况下更新视图,实现前端路由的功能。\[1\]\[2\] 另外,vue-router还支持懒加载的实现方式。最常用的懒加载方式是通过import()来实现。通过在路由配置中使用import()来动态加载组件,可以实现按需加载,提高应用的性能。例如,可以将组件的import语句放在路由配置中的component属性中,当路由被访问时,才会动态加载对应的组件。这种方式可以减少初始加载的资源量,提高应用的加载速度。\[3\] #### 引用[.reference_title] - *1* [vue-router实现原理](https://blog.csdn.net/mengweiping/article/details/101068638)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [超详细的vue-router原理](https://blog.csdn.net/jiangjialuan2/article/details/124799307)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Gjanuary

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值