Vue-路由

路由简介

SPA单页面应用。导航区和展示区

  1. 单页Web应用
  2. 整个应用只有一个完整的页面
  3. 点击页面中的导航连接不会刷新页面,只会做页面的局部更新
  4. 数据需要通过ajax请求获取

路由:路由就是一组映射关系,服务器接收到请求时,根据请求路径找到匹配的函数处理请求,返回响应数据

基本路由

main.js

import Vue from 'vue'
import App from './App.vue'

//1.下载vue-router模块到当前工程,版本3.6.5
//2.引入
import VueRouter from 'vue-router'

import ViewA from './views/ViewA.vue'
import ViewAA from './views/ViewAA.vue'
import ViewB from './views/ViewB.vue'
import ViewBB from './views/ViewBB.vue'

Vue.config.productionTip = false
//3.安装注册
Vue.use(VueRouter)
//4.创建路由对象
const router=new VueRouter({
  routes:[
    {
      path: '/viewA', 
      component :ViewA,
      children:
      [
        {
          path:'/viewAA',
          component:ViewAA
        }
      ]
    },
    {
      path:'/viewB',
      component:ViewB,
      children:
      [
        {
          path:'/viewBB',
          component: ViewBB
        }
      ]
    }
  ]
})

new Vue({
  render: h => h(App),
  //5.注册,将路由对象引入new Vue实例中,建立链接
  router
}).$mount('#app')

views/ViewA.vue

<template>
  <div>
    <h1>ViewA页面</h1>
    <a href="#/viewAA">ViewAA页面</a>
    <router-view></router-view>
  </div>
</template>

<script>
export default {
  name:'ViewPageA'
}
</script>

<style>

</style>

views/ViewAA.vue

<template>
  <div>
    <h1>ViewAA页面</h1>
  </div>
</template>

<script>
export default {
  name:'ViewPageAA'
}
</script>

<style>

</style>

views/ViewB.vue

<template>
  <div>
    <h1>ViewB页面</h1>
    <a href="#/viewBB">ViewBB页面</a>
    <router-view></router-view>
  </div>
</template>

<script>
export default {
  name:'ViewPageB'
}
</script>

<style>

</style>

views/ViewBB.vue

<template>
  <div>
    <h1>ViewBB页面</h1>
  </div>
</template>

<script>
export default {
  name:'ViewPageBB'
}
</script>

<style>

</style>

App.vue

<template>
  <div>
    <div>
      <a href="#/viewA">ViewA页面</a>
      <a href="#/viewB">ViewB页面</a>
    </div>
    <router-view></router-view>
  </div>
</template>

<script>
export default {

}
</script>

<style>

</style>

路由封装

1.将路由封装到一个js文件

2.模块导入改成绝对路径

注:ViewA.vue ViewAA.vue ViewB.vue ViewBB.vue App.vue与上面一样

封装 router/index.js

import Vue from 'vue'
import VueRouter from 'vue-router'
import ViewA from '@/views/ViewA.vue'
import ViewAA from '@/views/ViewAA.vue'
import ViewB from '@/views/ViewB.vue'
import ViewBB from '@/views/ViewBB.vue'
Vue.use(VueRouter)
const router = new VueRouter({
  routes: [
    {
      path: '/viewA',
      component: ViewA,
      children:
        [
          {
            path: '/viewAA',
            component: ViewAA
          }
        ]
    },
    {
      path: '/viewB',
      component: ViewB,
      children:
        [
          {
            path: '/viewBB',
            component: ViewBB
          }
        ]
    }
  ]
})
export default router

main.js

import Vue from 'vue'
import App from './App.vue'
import router  from '@/router/index'

console.log(router);

Vue.config.productionTip = false

new Vue({
  router,
  render: h => h(App)
}).$mount('#app')

router-link控件

router-link控件,用于代替a标签,里面有两个高亮类名 router-link-active 模糊匹配(用的多) router-link-exact-active 精准匹配 也可以自定义高亮类名

1.router-link-active

模糊匹配(用的多)

to=“/my” 可以匹配 /my /my/a /my/b …

只要是以/my开头的路径 都可以和 to="/my"匹配到

2.router-link-exact-active

精确匹配

to=“/my” 仅可以匹配 /my

const router = new VueRouter(
{ 
routes: 
[ 
 { path: '/my', component: My },
 { path: '/find', component: Find } 
], 
    // 模糊匹配,重新定义类名
    linkActiveClass: 'active', 
    // 精确匹配,重新定义类名
    linkExactActiveClass: 'exact-active' 
})

声明式导航-跳转传参

注:遇到页面可能需要参数可能不需要参数path写法->   path:'/search/:words?' 

编程式导航

this.$router.push('路径?参数名1=参数值')

获取值写法: this.$route.query.参数名1

this.$router.push({ path:'/路径', params:{ 参数名1:'参数值1', 参数名2:'参数值2' } })

获取值写法:

this.$route.params.参数名1

this.$route.params.参数名2

路由设置

 

组件缓存keep-alive

keep-alive用于对组件进行缓存,不在此进行重新加载 keep-alive的三个属性

1.include:组件名数组,只有匹配的组件会被缓存

2.exclude:组件名数组,任何匹配的组件都不会被缓存

3.最多可以缓存多少组件实例

4.使用会触发两个生命周期函数 activated:当组件被激活使用的时候触发->进入页面触发 deactivated:当组件不被使用的时候触发->离开页面触发,注:activated、deactivated两个方法在缓存组件创建

views/ViewA.vue

<template>
  <div>
    <h1>ViewA页面</h1>
  </div>
</template>

<script>
export default {
  name:'ViewPageA',
  created(){
    console.log('创建了A');
  },
  activated(){
    console.log('activated');
  },
  deactivated(){
    console.log('deactivated');
  }
}
</script>

<style>

</style>

views/ViewB.vue

<template>
  <div>
    <h1>ViewB页面</h1>
  </div>
</template>

<script>
export default {
  name: 'ViewPageB',
  created() {
    console.log('创建了B');
  }
}
</script>

<style>

</style>

App.vue

<template>
  <div>
    <div>
      <a href="#/viewA">ViewA页面</a>
      <a href="#/viewB">ViewB页面</a>
    </div>
    <keep-alive :include="['ViewPageA']">
      <router-view></router-view>
    </keep-alive>
  </div>
</template>

<script>
export default {
  data() {
    return {
      checked: true
    }
  }
}
</script>

<style></style>

  • 17
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue Router 是 Vue.js 官方的路由管理器,用于实现前端路由跳转。要进行路由跳转,你需要完成以下几个步骤: 1. 首先,确保你的项目中已经安装了 Vue Router。可以通过 npm 或 yarn 进行安装: ```bash npm install vue-router ``` 或 ```bash yarn add vue-router ``` 2. 在你的 Vue 项目的入口文件(一般是 `main.js`)中引入 Vue Router,并使用它: ```javascript import Vue from 'vue' import VueRouter from 'vue-router' // 导入你的路由配置文件 import routes from './routes' Vue.use(VueRouter) const router = new VueRouter({ mode: 'history', // 可选值为 'hash' 或 'history',默认为 'hash' routes // 路由配置 }) new Vue({ router, // 注入路由实例 render: h => h(App) }).$mount('#app') ``` 3. 创建一个路由配置文件(例如 `routes.js`),在该文件中定义路由的映射关系: ```javascript import Home from './views/Home.vue' import About from './views/About.vue' const routes = [ { path: '/', name: 'home', component: Home }, { path: '/about', name: 'about', component: About } ] export default routes ``` 4. 在你的 Vue 组件中使用 `<router-link>` 标签或 `$router` 对象进行路由跳转。下面是几个常用的示例: - 使用 `<router-link>` 标签实现路由跳转: ```html <router-link to="/">Home</router-link> <router-link to="/about">About</router-link> ``` - 使用 `$router` 对象编程式地进行路由跳转: ```javascript // 在某个方法中跳转到指定路由 this.$router.push('/') // 跳转到根路径 this.$router.push('/about') // 跳转到 /about 路径 // 在某个方法中通过路由名称跳转 this.$router.push({ name: 'home' }) // 跳转到名称为 home 的路由 ``` 这样,你就可以通过 Vue Router 实现路由跳转了。请注意,以上只是一个简单的示例,你可以根据实际需要配置更多高级功能,例如路由参数、嵌套路由等。详情请参考 Vue Router 的官方文档。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值