vue路由配置

news/2024/7/24 10:14:10 标签: javascript

1、安装路由,因为路由是vue的一个插件。

npm (cnpm) install vue-router -D

2、在main.js中引入路由

import Vue form 'vue'
import VueRouter from 'vue-router'
Vue.use(VueRouter)

3、在main.js中配置路由

clipboard.png

import Vue from 'vue'
import App from './App.vue'
// 引入路由
import VueRouter from 'vue-router'
Vue.use(VueRouter)

import Home from './Home'
import Me from './Me'

// 路由配置
const router = new VueRouter({
  mode: 'history',  // 使用h5的history模式
  base: __dirname, // 指定根目录
  linkActiveClass: 'active',
  routes: [
    {
      path: '/home', component: Home
    },
    {
      path: '/me', component: Me
    },
    {
      path: '*', redirect: Home
    }
  ]
})
new Vue({
  el: '#app',
  router, // 引入路由
  render: h => h(App)
})

4、很明显main.js中要引入喝多第三方插件,这样在main.js中配置路由是不合理的,所以需要在src目录中创建,config目录,里面创建routes.js路由文件

clipboard.png

router.js 代码

import Vue from 'vue'
import VueRouter from 'vue-router'
Vue.use(VueRouter)

import Home from '../Home'
import Me from '../Me'

export default new VueRouter ({
    mode: 'history',
    base: __dirname,
    routes: [
        {
            path: '/home', component: Home, name='home'
        },
        {
            path: '/me', component: Me, name='me'
            children: [ // 子路由
                { 
                    path: 'child-one',
                    component: ChildOne
                }
            ]
        },
        {
           path: '/books/:id', component: BookDetails, name='BookDetails'
        }
    ]
})

5、路由与导航
router-view:路由坐基
router-link

参数:
1、:to => :to="{name: 'home'}" 跳转的路由name
    :to="{name: 'BookDetails', params: {id: 1}}" 路由跳转参数
    获取:this.$route.params.id 就可以获取到参数
    export default {
        created() {
            const bookId = this.$route.params.id
        },
        watch: { // 参数路由组件的生命周期钩子函数不会再调用,created,mounted..., 所以如果要查看,需要监听
            '$route' (to, from) {
                对路由变化做出响应
            }
        }
    }
或者
to =>  to="/home" 跳转路由path

2、active-class="active" 路由激活样式,或者直接在路由定义的时候配置全局

new VueRouter({
    linkActiveClass: "active"
})

3、exact 精确匹配参数,这个很有用,在路由默认 "/"的时候,一般都加载到首页,也就是"/", "/home"其实加载的是同一个组件,但是问题来了,这样路由的激活样式就有问题了,不管跳转到哪个界面 "/"都是出于激活状态的,所以需要精确匹配,这样就解决了这个问题。
4、tag="div" 路由跳转时候生成html标签
5、history路由的控制,
    push() 默认 push到Url目录中 /a  => /b
    append() 追加到当前Url下    /a  => /a/b
    replace() 替换当前的url     /a  = /b
    
    <router-link :to="{name: 'Home'}" replace>替换</router-link>

6、路由懒加载

routes: [
        {
            path: '/home', component: Home, name: 'home'
        },
        {
            path: '/me', name: 'me',
            component:  resolve => require(['../Me.vue'], resolve) //懒加载
        }
    ]

7、app.vue

<template>
  <div id="app">
    <router-link to='home'>首页</router-link>
    <router-link to='me'>我</router-link>
    <router-view></router-view>
  </div>
</template>

<script>
import './assets/css/index.css' // 全局导入css文件
export default {
  name: "app",
  data() {
    return {
      msg: "hello world!"
    };
  }
};
</script>

// scoped 标识这个css样式只对这个组件有用
<style scoped> 
  /* 可以在里面写css 或者使用 @import 导入外部的css */
  @import './assets/css/index.css';
</style>

源码
github: https://github.com/zxc1989092...

案例:

clipboard.png

路由代码,实现懒加载

import Vue from 'vue'
import Router from 'vue-router'

import Home from '../components/Home'
import OrderList from '../components/OrderList'

Vue.use(Router)

export default new Router({
  base: __dirname,
  routes: [
    {
      path: '/home',
      name: 'Home',
      component: Home,
      redirect: '/home/order-list', // 重定向
      children: [
        {
          path: 'order-list',
          component: OrderList,
          name: 'order-list'
        },
        {
          path: 'user-list',
          component: resolve => require(['../components/UserList.vue'], resolve),
          name: 'user-list'
        },
        {
          path: 'engineer-list',
          component: resolve => require(['../components/EngineerList.vue'], resolve),
          name: 'engineer-list'
        }
      ]
    },
    {
      path: '/about',
      name: 'About',
      component: resolve => require(['../components/About.vue'], resolve)
    },
    {
      path: '/product',
      name: 'Product',
      component: resolve => require(['../components/Product.vue'], resolve)
    },
    {
      path: '/',
      redirect: 'home'
    }
  ]
})

目录结构

clipboard.png

源码地址
https://github.com/zxc1989092...


http://www.niftyadmin.cn/n/1120864.html

相关文章

iClap的名字是怎么来的,clap是有什么特殊的意义么?

iClap的名字来源于&#xff1a;Clap中文是鼓掌的意思&#xff0c;鼓掌代表合拍&#xff0c;一个团队的价值观以及工作方式合拍&#xff0c;是最重要的&#xff0c;当项目启动时&#xff0c;大家对产品认可&#xff0c;鼓掌开始实施&#xff1b;当项目成功上线&#xff0c;团队也…

使用MVCJqGrid的心得

最近公司网站进行升级&#xff0c;项目要用.net mvc&#xff0c;mysql和轻量级orm框架dapper。由于美工页面出不来啊&#xff0c;让我先写简单写写后台的列表&#xff0c;同事说用MvcJqGrid&#xff0c;也得到了架构的同意。 可是不得不说这个相关文档真不多啊&#xff0c;以前…

Swift中通过运行时实现全屏的pop功能

最近碰见个小的功能需求&#xff0c;就是需要给push的视图控制器添加一个侧滑pop弹出的功能&#xff0c;找了点资料&#xff0c;发现一个比较不错的解决办法&#xff0c;用了相识已久&#xff0c;但是没用过的运行时功能来实现。 具体思路如下&#xff1a; push到 UINavigation…

AWS认证权威考经(助理级认证篇)

笔者作为AWS官方认证的早期通过者&#xff0c;已经拿到了AWS的助理级解决方案架构师、开发者认证&#xff0c;系统管理员认证。这几年也陆续指导公司多人通过AWS的认证。本篇文章将分享如何通过自学的方式轻松通过AWS的助理级架构师、开发者和系统管理员认证。 为什么要考证 在…

文本文件内容相关命令

1、查看文本文件内容命令cat 命令格式&#xff1a;cat [option] filename cat命令常用的选项只有v&#xff0c;其功能是显示文件内容的同时&#xff0c;也显示文件中的控制字符&#xff0c;这个选项可以非常方便的查看脚本中不能识别的控制字符。 2、从文本尾查看文本内容命令t…

转载 - Linux TC(Traffic Control)框架原理解析

近日的工作多多少少和Linux的流控有点关系&#xff0c;自打几年前知道有TC这么一个玩意儿并且多多少少理解了它的原理之后&#xff0c;我就没有再动过它&#xff0c;因为我不喜欢TC命令行&#xff0c;实在是太繁琐了&#xff0c;iptables命令行也比较繁琐&#xff0c;但是比TC命…

Dynamics CRM 后台通过组织服务获取时间字段值的准确转换

做CRM开发的都知道&#xff0c;在系统时间字段的处理上是有讲究的&#xff0c;因为数据库中存的是UTC时间&#xff0c;CRM的界面时间字段会根据个人设置中的时区以及格式自动调整&#xff0c;这是最基本的一面&#xff0c;那还有很多使用时间的场景&#xff0c;比如脚本使用ODA…

算法----(3)选择排序

记录一趟中最大&#xff08;小&#xff09;元素&#xff0c;最后再进行交换 1 def select_sort(s):2 # select_sort3 for i in range(len(s) - 1):4 min i5 for j in range(i 1, len(s)):6 if s[min] > s[j]:7 min …