diff --git a/package.json b/package.json index 9ea6a7d..fdc355f 100644 --- a/package.json +++ b/package.json @@ -9,7 +9,8 @@ }, "dependencies": { "core-js": "^3.6.5", - "vue": "^2.6.11" + "vue": "^2.6.11", + "vue-router": "^3.5.1" }, "devDependencies": { "@vue/cli-plugin-babel": "~4.5.0", diff --git a/src/App.vue b/src/App.vue index 55df315..5f4cbfa 100644 --- a/src/App.vue +++ b/src/App.vue @@ -1,28 +1,58 @@ diff --git a/src/components/aa.vue b/src/components/aa.vue new file mode 100644 index 0000000..5394328 --- /dev/null +++ b/src/components/aa.vue @@ -0,0 +1,20 @@ + + + + diff --git a/src/components/item.vue b/src/components/item.vue new file mode 100644 index 0000000..1fc4aed --- /dev/null +++ b/src/components/item.vue @@ -0,0 +1,82 @@ + + + + + \ No newline at end of file diff --git a/src/main.js b/src/main.js index 63eb05f..c85821f 100644 --- a/src/main.js +++ b/src/main.js @@ -1,8 +1,19 @@ -import Vue from 'vue' -import App from './App.vue' - -Vue.config.productionTip = false - +import Vue from 'vue' // 引入 vue nodem_moudules 不需要写路径 +import App from './App.vue' // 自己的文件需要写路径 +import router from "./router/index.js" +Vue.config.productionTip = false // 阻止生产提示 new Vue({ - render: h => h(App), + render: h => h(App), // 渲染 传入的组件 根组件 + router: router //挂载router }).$mount('#app') +// 链式操作 vue3 $ + +// 引入完成 +// vue引入 Vue.use + +// 1.将组件映射到路由 +// 2.告诉vuerouter渲染到哪里 刚开始router 必须写在跟组件里面 (App.vue) +// 3. 创建 router 实例 // new 引入进来的router明总 +// 4. 创建和挂载根实例。 // 在new vue里面写 router: router + +// url带/#/ hash 导航 \ No newline at end of file diff --git a/src/pages/admin.vue b/src/pages/admin.vue new file mode 100644 index 0000000..3364716 --- /dev/null +++ b/src/pages/admin.vue @@ -0,0 +1,74 @@ + + + + + \ No newline at end of file diff --git a/src/pages/params.vue b/src/pages/params.vue new file mode 100644 index 0000000..76c01f2 --- /dev/null +++ b/src/pages/params.vue @@ -0,0 +1,22 @@ + + + + + \ No newline at end of file diff --git a/src/router/index.js b/src/router/index.js new file mode 100644 index 0000000..652b8d0 --- /dev/null +++ b/src/router/index.js @@ -0,0 +1,66 @@ +import Vue from 'vue' // 引入 vue nodem_moudules 不需要写路径 +import VueRouter from 'vue-router' // 引入vuroute +import index from "../views/index.vue" +import user from "../views/user.vue" +import admin from "../pages/admin.vue" +Vue.use(VueRouter) +const routes = [ + { path: '/', component: index }, + { path: '/a/b/c/d', name: "user", component: user }, + { + path: "/admin", beforeEnter: (to, from, next) => { + // ... + console.log("路由独享") + next() + }, component: admin + }, + { path: "/params", name: "params", component: () => import("../pages/params.vue") } +] +// path +// name 名字 +const router = new VueRouter({ + mode: "history", + routes // (缩写) 相当于 routes: routes +}) + +// 1.全局前置守卫 +// 2.路由独享守卫 +// 3.组件内解析守卫 +// 4. 解析守卫 +// 5. 全局后置守卫 + + +// 前置守卫 +router.beforeEach((to, from, next) => { + // console.log(to,from) + if (to.path == "/") { + // alert("你即将到首页") + console.log("你即将到首页") + } + if (to.path == "/a/b/c/d") { + console.log("到user") + next({ path: '/admin', query: { name: 123 } }) + } else { + next() + } + // ... + // next() // 继续跳转 + // next(false) 停止跳转 这次跳转不生效 + // next('url') or next({path,query,params,name}) 重定向到其他也页面 +}) +router.beforeResolve((to, from, next) => { + console.log("解析收尾") + next() + + // ... + // next() // 继续跳转 + // next(false) 停止跳转 这次跳转不生效 + // next('url') or next({path,query,params,name}) 重定向到其他也页面 +}) +// 后置 跳转完成之后 +router.afterEach((to, from) => { + console.log(to, from, "后置") + + // ... +}) +export default router \ No newline at end of file diff --git a/src/views/index.vue b/src/views/index.vue new file mode 100644 index 0000000..41a7125 --- /dev/null +++ b/src/views/index.vue @@ -0,0 +1,25 @@ + + + + + \ No newline at end of file diff --git a/src/views/user.vue b/src/views/user.vue new file mode 100644 index 0000000..c9d54ce --- /dev/null +++ b/src/views/user.vue @@ -0,0 +1,18 @@ + + + + + \ No newline at end of file diff --git a/yarn.lock b/yarn.lock index 5e3a538..d8b2f50 100644 --- a/yarn.lock +++ b/yarn.lock @@ -8179,6 +8179,11 @@ vue-loader@^15.9.2: vue-hot-reload-api "^2.3.0" vue-style-loader "^4.1.0" +vue-router@^3.5.1: + version "3.5.1" + resolved "https://registry.npm.taobao.org/vue-router/download/vue-router-3.5.1.tgz#edf3cf4907952d1e0583e079237220c5ff6eb6c9" + integrity sha1-7fPPSQeVLR4Fg+B5I3Igxf9utsk= + vue-style-loader@^4.1.0, vue-style-loader@^4.1.2: version "4.1.3" resolved "https://registry.npm.taobao.org/vue-style-loader/download/vue-style-loader-4.1.3.tgz#6d55863a51fa757ab24e89d9371465072aa7bc35"