Vue-router的使用和出现空白页
2018.08.28 更新
vue-router:前端路由系统——改变视图的同时不会向后端发出请求
1、 hash
2、history
2018.06.25 更新
get到一个新技能
import Vue from 'vue'import Router from 'vue-router'import api from '../lib/service' //接口文档Vue.use(Router)const router = { mode: 'history', routes: [{ chunkName: 'src/pages/index', name: 'index', path: '/', beforeEnter: async (to, from, next) => { await api.login().then((res) => { console.log(res) next() }).catch((rej) => { console.log('error') console.log(rej) next() }) }, component: () => import('../../src/pages/index.vue') }]}export default new Router(router)
beforeEnter:在加载路由时可以做一些事情,上面的代码执行的是,在加载之前调用登陆接口
2018 5.5 更新
空白页还有一种情况,页面中数据使用的错误,会导致空白页
可以带参数传路由,有兴趣的小伙伴可以试试
这个方法是我经常用的
this.$route.push({ path: '路径', query: { key: 'value' }})
跳转至另一个页面时,这样获取传的参数
this.$route.query.key
两种设计模式
history/hash
还有一些别的我记录的方法
$route.path
$route.params
$route.query
$route.hash
$route.matched //路由记录
$route.name
$route.fullPath //包含查询参数和hash完整路径
route.go(num)
router-link :to=”path”
//原来写的
自己之前跟着vue教学视频跟着老师一起打代码,现在为了配合课程设计准备自己写个vue项目,刚开始就在vue-router上遇到了小坎坷,就想分享一下
放上代码
main.js
import VueResource from 'vue-resource'import Index from './pages/index'import Content from './pages/content'import router from './router'import Router from 'vue-router'Vue.config.productionTip = falseVue.use(Router)Vue.use(VueResource)let routers = new Router({ mode: 'history', routes: [ { path: '/', component: Content, children: [ { path: '/content', component: Content } ] } ]})/* eslint-disable no-new */new Vue({ el: '#app', routers, template: '<Index/>', components: { Index }})
index.vue
<template> <div id="app" class="wrapper"> <div class="nav"> <ul> <li>首页</li> <li>技术文档</li> <li>留言</li> <li>关于我</li> </ul> </div> <div class="content"> <router-view></router-view> </div> <div class="footer"> @dwf </div> </div></template><script></script><style></style>
新闻热点
疑难解答
图片精选