登录  /  注册
首页 > web前端 > Vue.js > 正文

一文掌握vue-router的安装与使用

WBOY
发布: 2022-08-10 17:55:16
转载
1895人浏览过

本篇文章给大家带来了关于vuejs/" target="_blank">vue的相关知识,其中主要介绍了关于vue-router安装和使用的相关知识,下面一起来看一下,希望对大家有帮助。

一文掌握vue-router的安装与使用

【相关推荐:javascript视频教程vue.js教程

vue-router的安装与使用

一、安装

步骤1:安装vue-router

npm install vue-router --save
登录后复制

步骤2:在模块化工程中使用它(因为是一个插件,所以可以通过Vue.use()来安装路由功能)

  1. 导入路由对象,并调用Vue.use(VueRouter)

  2. 创建路由实例,并传入路由映射配置

  3. Vue实例挂载创建的路由实例


二、使用

在这里插入图片描述

创建的router的配置文件在src下的router文件夹下的index.js文件中

index.js中内容如下:

在这里插入图片描述

注:虽然在这里已经注册了router,但是其需要被挂在在vue上,才能起作用。

挂载方法:

在src文件下的main.js中引入router,并挂载在vue实例上。

//main.js
import Vue from 'vue'
import App from './App'
import router from './router'


Vue.config.productionTip = false

/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  render: h => h(App)
})
登录后复制

实际使用案例:

App.vue中关键配置如下:

<template>
  <div>
    <h2>我是app组件</h2>
    
    <!-- 通过router自带标签实现 -->
    <router-link>首页</router-link>
    <router-link>关于</router-link> 
    <router-link>用户</router-link>
    <router-link>档案</router-link>
    <!-- <router-link to=&#39;/home&#39; tag="button" replace>首页</router-link>
    <router-link to=&#39;/about&#39; tag="button" replace >关于</router-link>-->

    <!-- 通过代码跳转 -->
    <!-- 
    <button @click="homeClick">首页</button>
    <button @click="aboutClick">关于</button> 
    -->
    <keep-alive>
      <router-view></router-view>
    </keep-alive>

    <button>用户2</button>
    <button>档案</button>
  </div>
</template><script>
export default {
  name: "App",
  data(){
    return{
      userId:&#39;yzk&#39;
    }
  },
  methods: {
    aboutClick() {
      // 通过代码的方式修改路由 vue-router
      // 不能如下操作:此操作会绕过路由进行修改,违背初衷
      // history.pushState({},&#39;&#39;,&#39;home&#39;)
      // this.$router.push("/home");
      this.$router.replace("/home");
      console.log("about");
    },
    homeClick() {
      // this.$router.push("/about");
      this.$router.replace("/about");
      console.log("home");
    },
    userClick(){
      this.$router.push(&#39;/user/&#39;+this.userId);
    },
    profileClick(){
      this.$router.push({
        path:&#39;/profile&#39;,
        query:{
          name:&#39;kobe&#39;,
          age:18,
          height:1.98
        }
      })
    }
  },
};
</script><style>
.router-link-active {
  color: red;
}
.active {
  color: pink;
}
</style>
登录后复制

Router的index.js文件如下:

// 配置路由信息
import Vue from 'vue'
import VueRouter from 'vue-router'
// import Home from '../components/Home'
// import About from '../components/About'
// import User from '../components/User'

// 懒加载,提高效率(因为app.js文件中集成了所有的业务代码,因此请求事件可能较长
// 通过将app.js分隔,在需要使用某些js代码的时候,才接收其代码)
const Home = () =&gt; import('../components/Home')
const HomeNews = () =&gt; import('../components/HomeNews')
const HomeMessage = () =&gt; import('../components/HomeMessage')

const About = () =&gt; import('../components/About')
const User = () =&gt; import('../components/User')
const Profile = () =&gt; import('../components/Profile')



// 1.通过Vue.use(插件),安装插件
Vue.use(VueRouter)

// 2.创建VueRouter对象
const routes = [
  {
    path: '',
    //  component: Home
    // 重定向redirect
    redirect: '/home'
  },
  {
    path: '/home',
    component: Home,
    meta: { title: "首页" },
    children: [
      {
        path: '',
        redirect: 'news'
      },
      {
        path: 'news',
        //  注意这里是没有s的!!!
        component: HomeNews,

      },
      {
        path: 'message',
        component: HomeMessage
      },
    ]
  },
  {
    path: '/about',
    component: About,
    meta: { title: "关于" },
  },
  {
    path: '/user/:userId',
    component: User,
    meta: { title: "用户" },
  },
  {
    path: '/profile',
    component: Profile,
    meta: { title: "档案" },
  }
]
const router = new VueRouter({
  // 配置路由和组件间的映射关系
  routes,
  mode: 'history',
  linkActiveClass: 'active'
})

// 3.将router对象传入到Vue实例中
export default router

// 导航守卫 前置钩子
router.beforeEach((to, from, next) =&gt; {
  document.title = to.matched[0].meta.title
  console.log('+++');
  next()
})

// 导航守卫, 后置钩子 不需要调用next函数
router.afterEach((to,from) =&gt; {
  console.log('----');
})
登录后复制

main.js中的挂载:

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


Vue.config.productionTip = false

/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  render: h =&gt; h(App)
})
登录后复制

【相关推荐:javascript视频教程vue.js教程

以上就是一文掌握vue-router的安装与使用的详细内容,更多请关注php中文网其它相关文章!

智能AI问答
PHP中文网智能助手能迅速回答你的编程问题,提供实时的代码和解决方案,帮助你解决各种难题。不仅如此,它还能提供编程资源和学习指导,帮助你快速提升编程技能。无论你是初学者还是专业人士,AI智能助手都能成为你的可靠助手,助力你在编程领域取得更大的成就。
相关标签:
vue
来源:CSDN网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
关于CSS思维导图的课件在哪? 课件
凡人来自于2024-04-16 10:10:18
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2024 //m.sbmmt.com/ All Rights Reserved | php.cn | 湘ICP备2023035733号