Compare commits

...

12 Commits

Author SHA1 Message Date
230b444af8 meta 2021-07-09 11:09:25 +08:00
d75450ee48 导航首位 2021-07-08 17:56:54 +08:00
26f3b38c7e 404 2021-07-07 17:21:50 +08:00
4af8011e43 重定向 2021-07-07 17:15:13 +08:00
29736567e8 params 2021-07-07 17:08:47 +08:00
d3d64cd25e history 2021-07-07 11:51:18 +08:00
8c8b2a14ef 嵌套路由 2021-07-07 09:59:53 +08:00
d4d85a1f11 .go 2021-07-06 17:00:01 +08:00
f72a9aa2cb replace 跳转 2021-07-06 16:54:37 +08:00
b64bd050bd js 跳转传值 2021-07-06 16:49:04 +08:00
40784994d1 路由传值 2021-07-06 16:44:22 +08:00
727a943474 最基础的跳转 2021-07-06 16:36:32 +08:00
6 changed files with 181 additions and 10 deletions

View File

@ -3,7 +3,7 @@
"version": "0.1.0", "version": "0.1.0",
"private": true, "private": true,
"scripts": { "scripts": {
"serve": "vue-cli-service serve", "start": "vue-cli-service serve",
"build": "vue-cli-service build", "build": "vue-cli-service build",
"lint": "vue-cli-service lint" "lint": "vue-cli-service lint"
}, },

View File

@ -20,17 +20,81 @@ Vue.use(VueRouter) // vue 使用vuerouter
import bar from './pages/bar.vue' import bar from './pages/bar.vue'
import foo from './pages/foo.vue' import foo from './pages/foo.vue'
import index from "./pages/index.vue" import index from "./pages/index.vue"
import myqx from "./pages/myqx.vue"
// http://www.baidu.com/adsaw
// http 协议
// 127.0.0.1 ip or 域名
// www.baidu.com 域名 服务器
// /adsaw/asdxc url 网址路径
const routes = [ const routes = [
{ path: '/foo', component: foo },
{ path: '/bar', component: bar }, { path: '/foo', meta:{qx:'user'}, component: foo },
{ path: '/', component: index } { path: '/bar', meta:{qx:'user'}, component: bar },
{
path: '/index', meta:{qx:'user'}, component: index, children: [
{path:"myqx", meta:{qx:'user'}, component: myqx},
{
path: 'foo/:name/:age', meta:{qx:'user'} , name:"foo", component: foo
},
{ path: 'bar',meta:{qx:"admin"},beforeEnter(to, from, next){
console.log("这是独享")
next()
}, component: bar },
]
},
] ]
// router 路由对象 // router 路由对象
// history model js
const router = new VueRouter({ const router = new VueRouter({
mode: 'history',
routes: routes // (缩写) 相当于 routes: routes routes: routes // (缩写) 相当于 routes: routes
}) })
// 导航守卫
// beforeEach 导航前置守卫
router.beforeEach((to, from, next) => {
// ...
// to 到哪个 路由对象
// from 从哪来 路由对象
// console.log(to,"to")
// console.log(from,"from")
console.log(to.path)
console.log("前置")
if(to.meta.qx == "admin" || to.path == "/index/myqx"){
next()
}else{
next("/index/myqx")
}
// 为什么要执行
// next 如果不执行 就不会进行路由跳转 首次进来 白屏
})
// beforeResolve 导航解析守卫
router.beforeResolve((to, from, next) => {
// ...
// to 到哪个 路由对象
// from 从哪来 路由对象
// console.log(to,"to")
// console.log(from,"from")
console.log("解析")
next() // 为什么要执行
// next 如果不执行 就不会进行路由跳转 首次进来 白屏
})
// afterEach 全局后置钩子 跳转页面完成了
router.afterEach(() => {
// ...
// console.log(to,"到哪")
// console.log(from,"来自哪")
console.log("后置")
// alert("跳转完成了")
})
// http://localhost:8080/index/bar
// router 挂载路由 给vue实例 // router 挂载路由 给vue实例
new Vue({ new Vue({
//render 让vue 渲染 App //render 让vue 渲染 App

View File

@ -1,11 +1,31 @@
<template> <template>
<div>这是bar</div> <div>这是bar
<router-link to="/">到首页</router-link>
<button @click="chuancan">传参</button>
</div>
</template> </template>
<style scoped> <style scoped>
</style> </style>
<script> <script>
export default { export default {
beforeRouteEnter(to,from,next){
console.log("组件独享前置")
next()
},
beforeRouteLeave(to,from,next){
console.log("组件离开守卫")
next()
},
beforeRouteUpdate(to,from,next){
console.log("组件复用")
next()
},
methods:{
chuancan(){
console.log(this.$route.meta)
// this.$router.push({path: "/index/bar",query:{a: new Date().getTime()}})
}
}
} }
</script> </script>

View File

@ -1,11 +1,47 @@
<template> <template>
<div>这是foo</div> <div>这是foo
<router-link to="/bar">到bar</router-link>
<button @click="getzhi">获取传值</button>
<button @click="tiaozhuan">跳转</button>
<router-view></router-view>
</div>
</template> </template>
<style scoped> <style scoped>
</style> </style>
<script> <script>
export default { export default {
beforeRouteEnter(to, from, next) {
// confirm
// `this`
//
console.log("组件进入")
next()
},
beforeRouteUpdate(to,from,next){
// params query
console.log("组件复用")
next()
},
methods:{
getzhi(){
// let a = ""
// let b = 18;
// // let str = "" + a + "" + b
// //
// let str = `${a}${b}`
// console.log(str)
// url
// params url
let zhi = this.$route.meta
console.log(zhi)
},
tiaozhuan(){
this.$router.push({name:"foo",params:{name:new Date().getTime(),age:10}})
}
}
} }
</script> </script>

View File

@ -1,3 +1,51 @@
<template> <template>
<div>这是首页</div> <div style="display:flex">
</template> <div class="menu">
<el-menu
default-active="2"
class="el-menu-vertical-demo menu"
>
<el-menu-item index="2" @click="toindex">
<i class="el-icon-menu"></i>
<span slot="title">首页</span>
</el-menu-item>
<el-menu-item index="3" @click="tobar">
<i class="el-icon-document"></i>
<span slot="title">bar</span>
</el-menu-item>
<el-menu-item index="4" @click="tofoo">
<i class="el-icon-setting"></i>
<span slot="title">foo</span>
</el-menu-item>
</el-menu>
</div>
<router-view></router-view>
</div>
</template>
<script>
export default {
methods: {
toindex(){
this.$router.push("/index")
},
tobar(){
// "/index/bar" path
this.$router.push("/index/bar")
},
tofoo(){
// { name } name
// params 使 使 path
// this.$router.push({ path:"/index/foo",params:{name:"111"} })
this.$router.push({ name:"foo",params:{name:"aaaa",age:18} })
},
},
};
</script>
<style scoped>
.menu{
width:200px;
height: 100vh;
}
</style>

3
src/pages/myqx.vue Normal file
View File

@ -0,0 +1,3 @@
<template>
<div>你没有权限访问</div>
</template>