内容输出来源 拉勾教育大前端高薪训练营
import Vue from 'vue'
import VueRouter from 'vue-router' // 路由组件
import index from '@/views/index' // 组成插件
Vue.use(VueRouter)
// 路由规则
const routes = [
{
name: 'index',
path: '/',
component: index
} ]
// 路由对象
const router = new VueRouter({
routes
})
export default router
import Vue from 'vue'
import Index from '../views/Index.vue'
const routers = [
{
path: '/',
name: 'Index,
component: Index
},
{
path: '/detail/:id',
name: 'Detail',
// 开启props, 会把URL中的参数传递给组件
// 在组件中通过props来接收参数
props: true,
component: () => import('../views/Detail.vue')
}
]
// 获取参数
// 1 $route.params.id
// 2 通过开启props 获取id
export default {
name: 'Detail',
props: ['id']
}
{
path: '/',
component: Layout,
children: [
{
name: 'index',
path: '',
component: 'Index',
},
{
name: 'detail',
path: 'detail:id',
props: true,
component: () => import('@/views/Detail.vue')
}
]
}
<button @click="push">push</button>
<button @click="go">go</button>
export default {
name: 'Login',
methods: {
push() {
this.$router.push('/')
// 对象 路由名称
this.$router.push({name: 'Home'})
},
go() {
this.$router.go(-1)
}
}
}
{
path: '/',
component: Layout,
children: [
{
name: 'index',
path: '',
component: 'Index',
},
{
name: 'detail',
path: 'detail:id',
props: true,
component: () => import('@/views/Detail.vue')
},
{
name: '404',
path: '*',
props: true,
component: () => import('@/views/404.vue')
},
]
}
const router = new VueRouter({
mode: 'history',
routers
})
export default router
const path = require('path')
// 导入处理history 模式的模块
history = require('connect-history-api-fallback')
// 导入express
const express = require('express')
consty app = express()
// 注册处理history 模式的中间价
app.use(history)
// 处理静态资源的中间件,网站根目录 ../web
// 开启服务器,端口是3000
app.use(express.static(path.join(__dirname), '../web'))
app.listen(3000, () => {
console.log('服务器开启,端口3000')
})
// 在刷新的时候如果服务器没有找到页面,会默认把单页面应用的index.html返回浏览器
// 浏览器在加载的过程中会判断是否存在, 存在则跳转对应的页面
/*
* start nginx 启动
* nginx -s reload 重启
* nginx -s stop 停止
*/
// 刷新页面 服务器返回404页面
// 解决
nginx.conf 配置 try_files $uri/ /index.html
// 重启nginx
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
try_files $uri/ /index.html;
}
error_page 500 502 503 504 /50x.html
}
History 模式
前置知识
VueRouter 模拟实现 - 分析核心代码
// router/index.js
// 注册插件
Vue.use(VueRouter)
// 创建路由对象
const router = new VueRouter({
routers: [
{
name: 'Login',
path: '/',
component: () => import('@/views/Login.vue')
}
]
})
// main.js
// 创建vue 实例, 注册router对象
new Vue({
router,
render: h => h(App)
}).$mount('#app)
vue Router 类图
创建一个文件夹vueRouter/index.js
// 首先来实现intsall方法
let _Vue = null
export default class VueRouter {
static install (Vue) {
// 1 判断当前插件是否已经被安装
if (VueRouter.install.installed) {
return
}
_Vue = Vue
VueRouter.install.installed = true
// 2 把Vue构造函数记录到全局变量
// 3 把创建Vue 实例时候传入的router对象注入到Vue实例上
// 4 混入
_Vue.mixin({
beforeCreate () {
if (this.$options.router) {
_Vue.prototype.$router = this.$options.router
this.$options.router.init()
}
}
})
}
}
constructor (options) {
this.options = options
this.routeMap = {}
this.data = _Vue.observable({
current: '/'
})
}
作用
createRouterMap
这个对象上,options
上 createRouteMap () {
// 遍历所有的路由规则, 将路由规则解析成键值对形式存储在createRouterMap这个对象上,
// 所有的路由规则都在options 上
this.options.routes.forEach(route => {
this.routeMap[route.path] = route.component
})
}
init () {
this.createRouteMap()
this.initComponents(_Vue)
}
initComponents (Vue) {
// router-link 接收一个字符串参数to,也就是超链接的地址,
// routerlink最终渲染成超链接的内容在routerlink标签之间
Vue.component('router-link', {
props: {
to: String
},
template: ' '
})
}
// 在install中调用
this.$options.router.init()
修改router/index.js的vue官方router
// 使用刚刚自己写的
import VueRouter from '../vueRouter'
Vue的构建版本
template
模版,需要打包的时候提前编译我们可以在根目录下创建vue.config.js 文件进行配置解决这个问题
module.exports = {
runtimeCompiler: true
}
不使用vue.config.js来解决,将vue.config.js删除,重新编译
initComponents (Vue) {
// router-link 接收一个字符串参数to,也就是超链接的地址,
// routerlink最终渲染成超链接的内容在routerlink标签之间
Vue.component('router-link', {
props: {
to: String
},
render (h) {
return h('a', {
attrs: {
href: this.to
},
}, [this.$slots.default])
},
// template: ' '
})
}
initComponents (Vue) {
// router-link 接收一个字符串参数to,也就是超链接的地址,
// routerlink最终渲染成超链接的内容在routerlink标签之间
Vue.component('router-link', {
props: {
to: String
},
render (h) {
return h('a', {
attrs: {
href: this.to
},
on: {
click: this.clickHandler
}
}, [this.$slots.default])
},
methods: {
clickHandler (e) {
history.pushState({}, '', this.to)
this.$router.data.current = this.to
e.preventDefault()
}
}
// template: ' '
})
const self = this
Vue.component('router-view', {
render (h) {
const component = self.routeMap[self.data.current]
return h(component)
}
})
}
当我们点击浏览器的前进和后退按钮时,地址栏发生了变化,但是组件并没有变化,也就是说当我能点击前进或者后退时,我们并没有重新加载这个组件
initEvent () {
window.addEventListener('popstate', () => {
this.data.current = window.location.pathname
})
}
好啦,到这里完整的vue-router就写完啦~
完成代码请点击这里