Vue-router官网

安装

vue-router是一个插件包,所以我们还是需要用npm 来进行安装。打开命令行工具,进入你的项目目录,输入下面命令。

npm install vue-router --save

如果在一个模块化工程中使用它。必须通过Vue.use()明确地安装路由功能

注意:先引入vue,再引入vue-router

import Vue from 'vue'
import VueRouter from 'vue-router' //显示声明使用VueRouter
Vue.use(VueRouter);

测试

  1. 创建一个vue-cli程序 参考链接

  2. 安装 参考安装标题

  3. 新建组件src/components/Content.vue和Main.vue

Content.vue

<template>
<h1>内容</h1>
</template> <script>
export default {
name: "Content"
}
</script> <style scoped> </style>

Main.vue

<template>
<h1>主页</h1>
</template> <script>
export default {
name: "Main"
}
</script> <style scoped> </style>
  1. 在src下新建一个文件router/index.js,进行创建路由和路由映射

默认情况下,我们进入一个网站的首页,应该先把首页渲染。所以可以设置默认路径

import Vue from 'vue'
import VueRouter from 'vue-router'
//导入上面自定义的组件
import Content from "../components/Content";
import Main from "../components/Main";
//安装路由
Vue.use(VueRouter); //配置导出路由
export default new VueRouter({
routes:[
{
path: '/',
redirect: '/main'
},
{
//路由路径
path:'/content',
//跳转的组件
component:Content
},
{
//路由路径
path:'/main',
//跳转的组件
component:Main
},
]
});
  1. 在main.js中挂载路由
import Vue from 'vue'
import App from './App.vue'
import router from "./router"
Vue.config.productionTip = false new Vue({
render: h => h(App),
router
}).$mount('#app')
  1. 在App.vue中使用路由
<template>
<div id="app">
<img src="./assets/logo.png">
<HelloWorld/>
<router-link to="/main">首页</router-link>
<router-link to="/content">内容</router-link>
<router-view></router-view>
</div>
</template>
....
  1. 实现效果

我们会发现URL中存在#,那怎么消除呢?

localhost:8081/#/content

使用HTML5的history模式

默认情况下,路径的改变使用的是URL的hash,如果我们希望使用H5的History模式,可以进行如下配置mode: 'history'

import Vue from 'vue'
import VueRouter from 'vue-router'
//导入上面自定义的组件
import Content from "../components/Content";
import Main from "../components/Main";
//安装路由
Vue.use(VueRouter); //配置导出路由
export default new VueRouter({
routes:[
{
path: '/',
redirect: '/main'
},
{
//路由路径
path:'/content',
//跳转的组件
component:Content
},
{
//路由路径
path:'/main',
//跳转的组件
component:Main
},
],
mode: 'history'
});

最新文章

  1. 有些网站为什么要使用CDN,CDN又是什么呢
  2. iOS 10 的适配问题-b
  3. C#开发学习——ADO.NET几个重要对象
  4. android 在EditText中显示表情图片
  5. HTML5-常见的事件- beforeunload事件
  6. SecureCRT 6.7.1 注冊机 和谐 破解 补丁 方法
  7. [刷题]算法竞赛入门经典(第2版) 6-4/UVa439 6-5/UVa1600
  8. Linux驱动模型解析bus之platform bus
  9. 剑指Offer——银行网申内容模版
  10. java设计模式--简单工厂
  11. Linux下ps -ef和ps aux的区别
  12. 基于 docker 的yapi(快速部署)
  13. APPLE-SA-2019-3-25-5 iTunes 12.9.4 for Windows
  14. Oracle表空间不足;查询表空间使用率(unable to extend lob segment SYS_LOB0000076749C00006$$ by 8192 in tablespace USERS)
  15. 《ERP系统修正数据的sql文件》
  16. 【注册码】Matlab7.0(R14)注册码
  17. confirm消息对话框
  18. Vue的配置
  19. [py]python的继承体系-源码目录结构
  20. 使用 Eclipse 远程调试 Java 应用程序

热门文章

  1. Jmeter 分布式架构和服务器性能监控解决方案
  2. Qt update刷新之源码分析总结
  3. WorkSkill 面试之 字节跳动一面
  4. 2019 GDUT Rating Contest II : Problem B. Hoofball
  5. linux 安装FastFdfs
  6. filecoin今日价格,filecoin币价估值,filecoin币会涨到多少钱
  7. JAVA面试核心知识点(一):计算机网络
  8. 如何使用Docker部署Go Web应用
  9. Kubernetes,kubectl常用命令详解
  10. C++并发与多线程学习笔记--async、future、packaged_task、promise