2021前端技术面试必备Vue:(三)Router篇

<section id="nice" data-tool="mdnice编辑器" data-website="https://www.mdnice.com" style="padding: 0 10px; word-spacing: 0px; word-wrap: break-word; text-align: left; font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, 'PingFang SC', Cambria, Cochin, Georgia, Times, 'Times New Roman', serif; margin-top: -10px; line-height: 1.6; letter-spacing: .034em; color: rgb(63, 63, 63); font-size: 16px; word-break: all;"><blockquote data-tool="mdnice编辑器" style="font-size: 0.9em; overflow: auto; overflow-scrolling: touch; background: rgba(0, 0, 0, 0.05); color: #6a737d; padding-top: 10px; padding-bottom: 10px; padding-left: 20px; padding-right: 10px; margin-bottom: 20px; margin-top: 20px; padding: 15px 20px; line-height: 27px; background-color: rgb(239, 239, 239); border-left: none; display: block;"> <p style="padding-bottom: 8px; padding-top: 23px; margin: 0px; line-height: 26px; padding: 0px; font-size: 15px; color: rgb(89,89,89);"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">前两章陆续已经更新了Vue的基础入门篇文章和Vue组件篇文章,本章将更新Vue Router篇,前两篇掌握差不多了,你可以开发单页面应用,所有数据都在一页呈现出来,不能学习了本章文章后,你可以实现一个精美的Web应用了,本章将会介绍到路由的转换实现,路由的动态传递参数,路由的守卫...... 。</font></font></p> </blockquote> </section> <section id="nice" data-tool="mdnice编辑器" data-website="https://www.mdnice.com" style="padding: 0 10px; word-spacing: 0px; word-wrap: break-word; text-align: left; font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, 'PingFang SC', Cambria, Cochin, Georgia, Times, 'Times New Roman', serif; margin-top: -10px; line-height: 1.6; letter-spacing: .034em; color: rgb(63, 63, 63); font-size: 16px; word-break: all;"><h2 data-tool="mdnice编辑器" style="font-weight: bold; color: black; font-size: 22px; display: block; text-align: center; background-image: url(https://my-wechat.mdnice.com/mdnice/mountain_2_20191028221337.png); background-position: center center; background-repeat: no-repeat; background-attachment: initial; background-origin: initial; background-clip: initial; background-size: 63px; margin-top: 38px; margin-bottom: 10px;"><span class="prefix" style="display: none;"></span><span class="content" style="text-align: center; display: inline-block; height: 38px; line-height: 42px; color: rgb(60, 112, 198); background-position: left center; background-repeat: no-repeat; background-attachment: initial; background-origin: initial; background-clip: initial; background-size: 63px; margin-top: 38px; font-size: 18px; margin-bottom: 10px;"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">Vue路由器</font></font></span><span class="suffix"></span></h2> <h4 data-tool="mdnice编辑器" style="margin-top: 30px; margin-bottom: 15px; font-weight: bold; color: black; font-size: 18px;"><span class="prefix" style="display: none;"></span><span class="content"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">路由初体验</font></font></span><span class="suffix" style="display: none;"></span></h4> <h5 data-tool="mdnice编辑器" style="margin-top: 30px; margin-bottom: 15px; font-weight: bold; color: black; font-size: 16px;"><span class="prefix" style="display: none;"></span><span class="content"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">安装</font></font></span><span class="suffix" style="display: none;"></span></h5> <pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px;"><code class="hljs" style="overflow-x: auto; padding: 16px; background: #272822; color: #ddd; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; border-radius: 0px; font-size: 12px; -webkit-overflow-scrolling: touch;">npm install --save vue-router<br></code></pre> <h5 data-tool="mdnice编辑器" style="margin-top: 30px; margin-bottom: 15px; font-weight: bold; color: black; font-size: 16px;"><span class="prefix" style="display: none;"></span><span class="content"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">使用</font></font></span><span class="suffix" style="display: none;"></span></h5> <p data-tool="mdnice编辑器" style="font-size: 16px; padding-bottom: 8px; margin: 0; padding-top: 23px; color: rgb(74,74,74); line-height: 1.75em;"><code style="font-size: 14px; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; background-color: rgba(27,31,35,.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all; color: rgb(60, 112, 198);">router.js 路由配置</code></p> <pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px;"><code class="hljs" style="overflow-x: auto; padding: 16px; background: #272822; color: #ddd; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; border-radius: 0px; font-size: 12px; -webkit-overflow-scrolling: touch;">@ 如果你的Vue-Cli 是 <span class="hljs-number" style="line-height: 26px;">3</span> 的版本,可以在创建项目的同时,可以选择安装router 插件<font></font><br>vue ui <font></font><br><font></font><br><span class="hljs-number" style="line-height: 26px;">1.</span> 创建好项目,自带的router文件就是路由配置文件<font></font><br><font></font><br><font></font><br><font></font><br><span class="hljs-keyword" style="color: #f92672; font-weight: bold; line-height: 26px;">import</span> Vue <span class="hljs-keyword" style="color: #f92672; font-weight: bold; line-height: 26px;">from</span> <span class="hljs-string" style="color: #a6e22e; line-height: 26px;">'vue'</span><br><span class="hljs-comment" style="color: #75715e; line-height: 26px;">// 引入 vue-router</span><br><span class="hljs-keyword" style="color: #f92672; font-weight: bold; line-height: 26px;">import</span> VueRouter <span class="hljs-keyword" style="color: #f92672; font-weight: bold; line-height: 26px;">from</span> <span class="hljs-string" style="color: #a6e22e; line-height: 26px;">'vue-router'</span><br><span class="hljs-keyword" style="color: #f92672; font-weight: bold; line-height: 26px;">import</span> Father <span class="hljs-keyword" style="color: #f92672; font-weight: bold; line-height: 26px;">from</span> <span class="hljs-string" style="color: #a6e22e; line-height: 26px;">'../views/Father.vue'</span><font></font><br><font></font><br><span class="hljs-number" style="line-height: 26px;">2.</span> 在全局使用这个vue-router<font></font><br>Vue.use(VueRouter)<font></font><br><font></font><br><font></font><br><span class="hljs-number" style="line-height: 26px;">3.</span> 配置路由选项<br><span class="hljs-keyword" style="color: #f92672; font-weight: bold; line-height: 26px;">const</span> routes = [<font></font><br> {<font></font><br> <span class="hljs-attr" style="line-height: 26px;">path</span>: <span class="hljs-string" style="color: #a6e22e; line-height: 26px;">'/'</span>,<br> <span class="hljs-attr" style="line-height: 26px;">name</span>: <span class="hljs-string" style="color: #a6e22e; line-height: 26px;">'Father'</span>,<br> <span class="hljs-attr" style="line-height: 26px;">component</span>: Father,<br> <span class="hljs-attr" style="line-height: 26px;">meta</span>: {<br> <span class="hljs-attr" style="line-height: 26px;">keepAlive</span>: <span class="hljs-literal" style="color: #f92672; font-weight: bold; line-height: 26px;">true</span> <span class="hljs-comment" style="color: #75715e; line-height: 26px;">// 需要缓存</span><font></font><br> }<font></font><br> }<font></font><br>]<font></font><br><font></font><br><span class="hljs-number" style="line-height: 26px;">3.</span> 创建Vue-router实例,挂载router配置项<br><span class="hljs-keyword" style="color: #f92672; font-weight: bold; line-height: 26px;">const</span> router = <span class="hljs-keyword" style="color: #f92672; font-weight: bold; line-height: 26px;">new</span> VueRouter({<font></font><br> routes<font></font><br>})<font></font><br><font></font><br><span class="hljs-number" style="line-height: 26px;">4.</span>最后导出 Vue-router实例, 提供给 Vue 挂载使用<br><span class="hljs-keyword" style="color: #f92672; font-weight: bold; line-height: 26px;">export</span> <span class="hljs-keyword" style="color: #f92672; font-weight: bold; line-height: 26px;">default</span> router<font></font><br><font></font><br><font></font><br></code></pre> <p data-tool="mdnice编辑器" style="font-size: 16px; padding-bottom: 8px; margin: 0; padding-top: 23px; color: rgb(74,74,74); line-height: 1.75em;"><code style="font-size: 14px; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; background-color: rgba(27,31,35,.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all; color: rgb(60, 112, 198);">index.js Vue入口文件配置</code></p> <pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px;"><code class="hljs" style="overflow-x: auto; padding: 16px; background: #272822; color: #ddd; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; border-radius: 0px; font-size: 12px; -webkit-overflow-scrolling: touch;"><span class="hljs-keyword" style="color: #f92672; font-weight: bold; line-height: 26px;">import</span> Vue <span class="hljs-keyword" style="color: #f92672; font-weight: bold; line-height: 26px;">from</span> <span class="hljs-string" style="color: #a6e22e; line-height: 26px;">'vue'</span><br><span class="hljs-keyword" style="color: #f92672; font-weight: bold; line-height: 26px;">import</span> App <span class="hljs-keyword" style="color: #f92672; font-weight: bold; line-height: 26px;">from</span> <span class="hljs-string" style="color: #a6e22e; line-height: 26px;">'./App.vue'</span><br><span class="hljs-number" style="line-height: 26px;">1.</span> 引入 路由配置文件<br><span class="hljs-keyword" style="color: #f92672; font-weight: bold; line-height: 26px;">import</span> router <span class="hljs-keyword" style="color: #f92672; font-weight: bold; line-height: 26px;">from</span> <span class="hljs-string" style="color: #a6e22e; line-height: 26px;">'./router'</span><br><span class="hljs-keyword" style="color: #f92672; font-weight: bold; line-height: 26px;">import</span> store <span class="hljs-keyword" style="color: #f92672; font-weight: bold; line-height: 26px;">from</span> <span class="hljs-string" style="color: #a6e22e; line-height: 26px;">'./store'</span><font></font><br><font></font><br>Vue.config.productionTip = <span class="hljs-literal" style="color: #f92672; font-weight: bold; line-height: 26px;">false</span><font></font><br><font></font><br><span class="hljs-number" style="line-height: 26px;">2.</span> 将路由挂载到 Vue实例中使用。<br><span class="hljs-keyword" style="color: #f92672; font-weight: bold; line-height: 26px;">new</span> Vue({<font></font><br> router,<font></font><br> store,<font></font><br> <span class="hljs-attr" style="line-height: 26px;">render</span>: <span class="hljs-function" style="line-height: 26px;"><span class="hljs-params" style="line-height: 26px;">h</span> =&gt;</span> h(App)<font></font><br>}).$mount(<span class="hljs-string" style="color: #a6e22e; line-height: 26px;">'#app'</span>)<font></font><br><font></font><br></code></pre> <h4 data-tool="mdnice编辑器" style="margin-top: 30px; margin-bottom: 15px; font-weight: bold; color: black; font-size: 18px;"><span class="prefix" style="display: none;"></span><span class="content"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">动态路由传递参数</font></font></span><span class="suffix" style="display: none;"></span></h4> <pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px;"><code class="hljs" style="overflow-x: auto; padding: 16px; background: #272822; color: #ddd; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; border-radius: 0px; font-size: 12px; -webkit-overflow-scrolling: touch;"><span class="hljs-keyword" style="color: #f92672; font-weight: bold; line-height: 26px;">const</span> routes = [<font></font><br> {<font></font><br> <span class="hljs-number" style="line-height: 26px;">1.</span> 通过在url后:参数即可<font></font><br> path: <span class="hljs-string" style="color: #a6e22e; line-height: 26px;">'/home/:name'</span>,<br> <span class="hljs-attr" style="line-height: 26px;">name</span>: <span class="hljs-string" style="color: #a6e22e; line-height: 26px;">'home'</span>,<br> <span class="hljs-attr" style="line-height: 26px;">component</span>: Father,<br> <span class="hljs-attr" style="line-height: 26px;">meta</span>: {<br> <span class="hljs-attr" style="line-height: 26px;">keepAlive</span>: <span class="hljs-literal" style="color: #f92672; font-weight: bold; line-height: 26px;">true</span> <span class="hljs-comment" style="color: #75715e; line-height: 26px;">// 需要缓存</span><font></font><br> }<font></font><br> }<font></font><br>]<font></font><br><font></font><br><font></font><br><font></font><br><span class="hljs-number" style="line-height: 26px;">2.</span>接收路由参数: <span class="hljs-keyword" style="color: #f92672; font-weight: bold; line-height: 26px;">this</span>.$route.params.路由参数<br></code></pre> <h4 data-tool="mdnice编辑器" style="margin-top: 30px; margin-bottom: 15px; font-weight: bold; color: black; font-size: 18px;"><span class="prefix" style="display: none;"></span><span class="content"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">捕获404页面</font></font></span><span class="suffix" style="display: none;"></span></h4> <pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px;"><code class="hljs" style="overflow-x: auto; padding: 16px; background: #272822; color: #ddd; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; border-radius: 0px; font-size: 12px; -webkit-overflow-scrolling: touch;">{<br> <span class="hljs-comment" style="color: #75715e; line-height: 26px;">// 会匹配所有路径</span><br> path: <span class="hljs-string" style="color: #a6e22e; line-height: 26px;">'*'</span><font></font><br>}<font></font><br>{<font></font><br> <span class="hljs-comment" style="color: #75715e; line-height: 26px;">// 会匹配以 `/user-` 开头的任意路径</span><br> path: <span class="hljs-string" style="color: #a6e22e; line-height: 26px;">'/user-*'</span><font></font><br>}<font></font><br><font></font><br><font></font><br>当使用一个通配符时,$route.params 内会自动添加一个名为 pathMatch 参数。它包含了 URL 通过通配符被匹配的部分:<font></font><br><font></font><br><span class="hljs-number" style="line-height: 26px;">1.</span> <span class="hljs-keyword" style="color: #f92672; font-weight: bold; line-height: 26px;">this</span>.$route.params.pathMatch 来获取通配符后的url<br></code></pre> <h4 data-tool="mdnice编辑器" style="margin-top: 30px; margin-bottom: 15px; font-weight: bold; color: black; font-size: 18px;"><span class="prefix" style="display: none;"></span><span class="content"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">嵌套路由</font></font></span><span class="suffix" style="display: none;"></span></h4> <pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px;"><code class="hljs" style="overflow-x: auto; padding: 16px; background: #272822; color: #ddd; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; border-radius: 0px; font-size: 12px; -webkit-overflow-scrolling: touch;"><span class="hljs-number" style="line-height: 26px;">1.</span> 通过在父路由中添加 children 数组,直接引入嵌套组件和配置path<br><span class="hljs-number" style="line-height: 26px;">2.</span> 页面中使用 通过 &lt;router-link to=<span class="hljs-string" style="color: #a6e22e; line-height: 26px;">"/父路由path/嵌套path"</span>&gt;小红&lt;<span class="hljs-regexp" style="color: #bf79db; line-height: 26px;">/router-link&gt;<br>3. 配置显示入口 &lt;router-view&gt;&lt;/</span>router-view&gt;<font></font><br><font></font><br>{<font></font><br> <span class="hljs-attr" style="line-height: 26px;">path</span>: <span class="hljs-string" style="color: #a6e22e; line-height: 26px;">'/teacher'</span>,<br> <span class="hljs-attr" style="line-height: 26px;">name</span>: <span class="hljs-string" style="color: #a6e22e; line-height: 26px;">'teacher'</span>,<br> <span class="hljs-attr" style="line-height: 26px;">component</span>: <span class="hljs-function" style="line-height: 26px;"><span class="hljs-params" style="line-height: 26px;">()</span> =&gt;</span> <span class="hljs-keyword" style="color: #f92672; font-weight: bold; line-height: 26px;">import</span>(<span class="hljs-string" style="color: #a6e22e; line-height: 26px;">'../views/Teacher/Teacher.vue'</span>),<br> <span class="hljs-attr" style="line-height: 26px;">children</span>: [<font></font><br> {<font></font><br> <span class="hljs-attr" style="line-height: 26px;">path</span>: <span class="hljs-string" style="color: #a6e22e; line-height: 26px;">'xiaohong'</span>,<br> <span class="hljs-attr" style="line-height: 26px;">component</span>: <span class="hljs-function" style="line-height: 26px;"><span class="hljs-params" style="line-height: 26px;">()</span> =&gt;</span> <span class="hljs-keyword" style="color: #f92672; font-weight: bold; line-height: 26px;">import</span>(<span class="hljs-string" style="color: #a6e22e; line-height: 26px;">'../views/Teacher/XiaoHong.vue'</span>)<font></font><br> },<font></font><br> {<font></font><br> <span class="hljs-attr" style="line-height: 26px;">path</span>: <span class="hljs-string" style="color: #a6e22e; line-height: 26px;">'xiaoming'</span>,<br> <span class="hljs-attr" style="line-height: 26px;">component</span>: <span class="hljs-function" style="line-height: 26px;"><span class="hljs-params" style="line-height: 26px;">()</span> =&gt;</span> <span class="hljs-keyword" style="color: #f92672; font-weight: bold; line-height: 26px;">import</span>(<span class="hljs-string" style="color: #a6e22e; line-height: 26px;">'../views/Teacher/XiaoMing.vue'</span>)<font></font><br> }<font></font><br> ]<font></font><br> }<font></font><br></code></pre> <img src="https://user-gold-cdn.xitu.io/2020/3/29/1712690aa53425e8?w=1110&h=450&f=gif&s=138040" data-tool="mdnice编辑器" style="display: block; margin: 0 auto; width: auto; max-width: 100%; border-radius: 4px; margin-bottom: 25px;"> <h4 data-tool="mdnice编辑器" style="margin-top: 30px; margin-bottom: 15px; font-weight: bold; color: black; font-size: 18px;"><span class="prefix" style="display: none;"></span><span class="content"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">编程式导航</font></font></span><span class="suffix" style="display: none;"></span></h4> <blockquote data-tool="mdnice编辑器" style="font-size: 0.9em; overflow: auto; overflow-scrolling: touch; background: rgba(0, 0, 0, 0.05); color: #6a737d; padding-top: 10px; padding-bottom: 10px; padding-left: 20px; padding-right: 10px; margin-bottom: 20px; margin-top: 20px; padding: 15px 20px; line-height: 27px; background-color: rgb(239, 239, 239); border-left: none; display: block;"> <p style="padding-bottom: 8px; padding-top: 23px; margin: 0px; line-height: 26px; padding: 0px; font-size: 15px; color: rgb(89,89,89);"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">导航分为:</font></font></p> <p style="padding-bottom: 8px; padding-top: 23px; margin: 0px; line-height: 26px; padding: 0px; font-size: 15px; color: rgb(89,89,89);"><code style="font-size: 14px; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; background-color: rgba(27,31,35,.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all; color: rgb(60, 112, 198);">router.push</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">来实现编程式导航</font></font></p> <p style="padding-bottom: 8px; padding-top: 23px; margin: 0px; line-height: 26px; padding: 0px; font-size: 15px; color: rgb(89,89,89);"><code style="font-size: 14px; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; background-color: rgba(27,31,35,.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all; color: rgb(60, 112, 198);">&lt;router-link&gt;</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;"> 声明式导航创建标签来定义导航链接</font></font></p> <p style="padding-bottom: 8px; padding-top: 23px; margin: 0px; line-height: 26px; padding: 0px; font-size: 15px; color: rgb(89,89,89);"><code style="font-size: 14px; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; background-color: rgba(27,31,35,.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all; color: rgb(60, 112, 198);">router.push</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">会向历史记录栈添加一个新的记录,所以,当用户点击浏览器后退按钮时,则回到之前的URL。</font></font></p> </blockquote> <h5 data-tool="mdnice编辑器" style="margin-top: 30px; margin-bottom: 15px; font-weight: bold; color: black; font-size: 16px;"><span class="prefix" style="display: none;"></span><span class="content"><code>router.push</code></span><span class="suffix" style="display: none;"></span></h5> <pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px;"><code class="hljs" style="overflow-x: auto; padding: 16px; background: #272822; color: #ddd; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; border-radius: 0px; font-size: 12px; -webkit-overflow-scrolling: touch;"><span class="hljs-comment" style="color: #75715e; line-height: 26px;">// 字符串</span><br>router.push(<span class="hljs-string" style="color: #a6e22e; line-height: 26px;">'home'</span>)<font></font><br><font></font><br><span class="hljs-comment" style="color: #75715e; line-height: 26px;">// 对象</span><br>router.push({ <span class="hljs-attr" style="line-height: 26px;">path</span>: <span class="hljs-string" style="color: #a6e22e; line-height: 26px;">'home'</span> })<font></font><br><font></font><br><span class="hljs-comment" style="color: #75715e; line-height: 26px;">// 命名的路由</span><br>router.push({ <span class="hljs-attr" style="line-height: 26px;">name</span>: <span class="hljs-string" style="color: #a6e22e; line-height: 26px;">'user'</span>, <span class="hljs-attr" style="line-height: 26px;">params</span>: { <span class="hljs-attr" style="line-height: 26px;">userId</span>: <span class="hljs-string" style="color: #a6e22e; line-height: 26px;">'123'</span> }})<font></font><br><font></font><br><span class="hljs-comment" style="color: #75715e; line-height: 26px;">// 带查询参数,变成 /register?plan=private</span><br>router.push({ <span class="hljs-attr" style="line-height: 26px;">path</span>: <span class="hljs-string" style="color: #a6e22e; line-height: 26px;">'register'</span>, <span class="hljs-attr" style="line-height: 26px;">query</span>: { <span class="hljs-attr" style="line-height: 26px;">plan</span>: <span class="hljs-string" style="color: #a6e22e; line-height: 26px;">'private'</span> }})<font></font><br><font></font><br>router.push({ <span class="hljs-attr" style="line-height: 26px;">name</span>: <span class="hljs-string" style="color: #a6e22e; line-height: 26px;">'user'</span>, <span class="hljs-attr" style="line-height: 26px;">params</span>: { userId }}) <span class="hljs-comment" style="color: #75715e; line-height: 26px;">// -&gt; /user/123</span><br>router.push({ <span class="hljs-attr" style="line-height: 26px;">path</span>: <span class="hljs-string" style="color: #a6e22e; line-height: 26px;">`/user/<span class="hljs-subst" style="color: #a6e22e; line-height: 26px;">${userId}</span>`</span> }) <span class="hljs-comment" style="color: #75715e; line-height: 26px;">// -&gt; /user/123</span><br></code></pre> <h5 data-tool="mdnice编辑器" style="margin-top: 30px; margin-bottom: 15px; font-weight: bold; color: black; font-size: 16px;"><span class="prefix" style="display: none;"></span><span class="content"><code>router.replace</code></span><span class="suffix" style="display: none;"></span></h5> <blockquote data-tool="mdnice编辑器" style="font-size: 0.9em; overflow: auto; overflow-scrolling: touch; background: rgba(0, 0, 0, 0.05); color: #6a737d; padding-top: 10px; padding-bottom: 10px; padding-left: 20px; padding-right: 10px; margin-bottom: 20px; margin-top: 20px; padding: 15px 20px; line-height: 27px; background-color: rgb(239, 239, 239); border-left: none; display: block;"> <p style="padding-bottom: 8px; padding-top: 23px; margin: 0px; line-height: 26px; padding: 0px; font-size: 15px; color: rgb(89,89,89);"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">它不会向历史添加新记录,它会替换当前路径</font></font></p> </blockquote> <pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px;"><code class="hljs" style="overflow-x: auto; padding: 16px; background: #272822; color: #ddd; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; border-radius: 0px; font-size: 12px; -webkit-overflow-scrolling: touch;"><span class="hljs-number" style="line-height: 26px;">1.</span>声明式写法<font></font><br>&lt;router-link :to=<span class="hljs-string" style="color: #a6e22e; line-height: 26px;">"..."</span> replace&gt;<font></font><br> <font></font><br><span class="hljs-number" style="line-height: 26px;">2.</span>编程式写法<font></font><br>router.replace(...)<font></font><br></code></pre> <h5 data-tool="mdnice编辑器" style="margin-top: 30px; margin-bottom: 15px; font-weight: bold; color: black; font-size: 16px;"><span class="prefix" style="display: none;"></span><span class="content"><code>router.go(n)</code></span><span class="suffix" style="display: none;"></span></h5> <blockquote data-tool="mdnice编辑器" style="font-size: 0.9em; overflow: auto; overflow-scrolling: touch; background: rgba(0, 0, 0, 0.05); color: #6a737d; padding-top: 10px; padding-bottom: 10px; padding-left: 20px; padding-right: 10px; margin-bottom: 20px; margin-top: 20px; padding: 15px 20px; line-height: 27px; background-color: rgb(239, 239, 239); border-left: none; display: block;"> <p style="padding-bottom: 8px; padding-top: 23px; margin: 0px; line-height: 26px; padding: 0px; font-size: 15px; color: rgb(89,89,89);"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">路由的前进和后退,前进合并整数,后退预定负数</font></font></p> </blockquote> <h4 data-tool="mdnice编辑器" style="margin-top: 30px; margin-bottom: 15px; font-weight: bold; color: black; font-size: 18px;"><span class="prefix" style="display: none;"></span><span class="content"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">命名路由</font></font></span><span class="suffix" style="display: none;"></span></h4> <blockquote data-tool="mdnice编辑器" style="font-size: 0.9em; overflow: auto; overflow-scrolling: touch; background: rgba(0, 0, 0, 0.05); color: #6a737d; padding-top: 10px; padding-bottom: 10px; padding-left: 20px; padding-right: 10px; margin-bottom: 20px; margin-top: 20px; padding: 15px 20px; line-height: 27px; background-color: rgb(239, 239, 239); border-left: none; display: block;"> <p style="padding-bottom: 8px; padding-top: 23px; margin: 0px; line-height: 26px; padding: 0px; font-size: 15px; color: rgb(89,89,89);"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">所谓命名路由,给路由配置名称属性,然后在页面中也可以使用 </font></font><code style="font-size: 14px; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; background-color: rgba(27,31,35,.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all; color: rgb(60, 112, 198);">this.$router.push({ name: 'home',{params:name='Test'}})</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">进行路由转换传递参数,很方便。</font></font></p> </blockquote> <pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px;"><code class="hljs" style="overflow-x: auto; padding: 16px; background: #272822; color: #ddd; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; border-radius: 0px; font-size: 12px; -webkit-overflow-scrolling: touch;"><span class="hljs-keyword" style="color: #f92672; font-weight: bold; line-height: 26px;">const</span> routes = [<font></font><br> {<font></font><br> <span class="hljs-attr" style="line-height: 26px;">path</span>: <span class="hljs-string" style="color: #a6e22e; line-height: 26px;">'/home:name'</span>,<br> <span class="hljs-attr" style="line-height: 26px;">name</span>: <span class="hljs-string" style="color: #a6e22e; line-height: 26px;">'home'</span>,<br> <span class="hljs-attr" style="line-height: 26px;">component</span>: Father,<font></font><br> }<font></font><br>]<font></font><br><font></font><br><font></font><br><font></font><br><span class="hljs-number" style="line-height: 26px;">1</span> 通过命名路由来实现 路由跳转和传递参数<br><span class="hljs-keyword" style="color: #f92672; font-weight: bold; line-height: 26px;">this</span>.$router.push({ <span class="hljs-attr" style="line-height: 26px;">name</span>: <span class="hljs-string" style="color: #a6e22e; line-height: 26px;">'home'</span>,{<span class="hljs-attr" style="line-height: 26px;">params</span>:name=<span class="hljs-string" style="color: #a6e22e; line-height: 26px;">'Test'</span>}})<br></code></pre> <h4 data-tool="mdnice编辑器" style="margin-top: 30px; margin-bottom: 15px; font-weight: bold; color: black; font-size: 18px;"><span class="prefix" style="display: none;"></span><span class="content"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">重新重定向</font></font></span><span class="suffix" style="display: none;"></span></h4> <pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px;"><code class="hljs" style="overflow-x: auto; padding: 16px; background: #272822; color: #ddd; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; border-radius: 0px; font-size: 12px; -webkit-overflow-scrolling: touch;"><span class="hljs-number" style="line-height: 26px;">1.</span> redirect: <span class="hljs-string" style="color: #a6e22e; line-height: 26px;">'/b'</span><br><span class="hljs-number" style="line-height: 26px;">2.</span> 重定向的目标也可以是一个命名的路由: redirect: { <span class="hljs-attr" style="line-height: 26px;">name</span>: <span class="hljs-string" style="color: #a6e22e; line-height: 26px;">'foo'</span> }<br></code></pre> <h4 data-tool="mdnice编辑器" style="margin-top: 30px; margin-bottom: 15px; font-weight: bold; color: black; font-size: 18px;"><span class="prefix" style="display: none;"></span><span class="content"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">路由守卫</font></font></span><span class="suffix" style="display: none;"></span></h4> <blockquote data-tool="mdnice编辑器" style="font-size: 0.9em; overflow: auto; overflow-scrolling: touch; background: rgba(0, 0, 0, 0.05); color: #6a737d; padding-top: 10px; padding-bottom: 10px; padding-left: 20px; padding-right: 10px; margin-bottom: 20px; margin-top: 20px; padding: 15px 20px; line-height: 27px; background-color: rgb(239, 239, 239); border-left: none; display: block;"> <p style="padding-bottom: 8px; padding-top: 23px; margin: 0px; line-height: 26px; padding: 0px; font-size: 15px; color: rgb(89,89,89);"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">以以着(或)者(其),(、、、、、、、、、、、、(,),定向到登陆页。</font></font></p> </blockquote> <pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px;"><code class="hljs" style="overflow-x: auto; padding: 16px; background: #272822; color: #ddd; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; border-radius: 0px; font-size: 12px; -webkit-overflow-scrolling: touch;"><br>router.beforeEach(<span class="hljs-function" style="line-height: 26px;">(<span class="hljs-params" style="line-height: 26px;">to, <span class="hljs-keyword" style="color: #f92672; font-weight: bold; line-height: 26px;">from</span>, next</span>) =&gt;</span> {<br> <span class="hljs-comment" style="color: #75715e; line-height: 26px;">//我在这里模仿了一个获取用户信息的方法</span><br> <span class="hljs-keyword" style="color: #f92672; font-weight: bold; line-height: 26px;">let</span> isLogin = <span class="hljs-built_in" style="color: #a6e22e; line-height: 26px;">window</span>.sessionStorage.getItem(<span class="hljs-string" style="color: #a6e22e; line-height: 26px;">'userInfo'</span>);<br> <span class="hljs-keyword" style="color: #f92672; font-weight: bold; line-height: 26px;">if</span> (isLogin) {<br> <span class="hljs-comment" style="color: #75715e; line-height: 26px;">//如果用户信息存在则往下执行。</span><font></font><br> next()<font></font><br> } <span class="hljs-keyword" style="color: #f92672; font-weight: bold; line-height: 26px;">else</span> {<br> <span class="hljs-comment" style="color: #75715e; line-height: 26px;">//如果用户token不存在则跳转到login页面</span><br> <span class="hljs-keyword" style="color: #f92672; font-weight: bold; line-height: 26px;">if</span> (to.path === <span class="hljs-string" style="color: #a6e22e; line-height: 26px;">'/login'</span>) {<font></font><br> next()<font></font><br> } <span class="hljs-keyword" style="color: #f92672; font-weight: bold; line-height: 26px;">else</span> {<font></font><br> next(<span class="hljs-string" style="color: #a6e22e; line-height: 26px;">'/login'</span>)<font></font><br> }<font></font><br> } <font></font><br>})<font></font><br></code></pre> <h4 data-tool="mdnice编辑器" style="margin-top: 30px; margin-bottom: 15px; font-weight: bold; color: black; font-size: 18px;"><span class="prefix" style="display: none;"></span><span class="content"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">路由过渡特效</font></font></span><span class="suffix" style="display: none;"></span></h4> <pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px;"><code class="hljs" style="overflow-x: auto; padding: 16px; background: #272822; color: #ddd; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; border-radius: 0px; font-size: 12px; -webkit-overflow-scrolling: touch;"><span class="hljs-number" style="line-height: 26px;">1.</span>给路由入口加入 transition<font></font><br><font></font><br><font></font><br>&lt;transition&gt;<font></font><br> <span class="xml" style="line-height: 26px;"><span class="hljs-tag" style="color: #f92672; line-height: 26px;">&lt;<span class="hljs-name" style="color: #f92672; line-height: 26px;">router-view</span>&gt;</span><span class="hljs-tag" style="color: #f92672; line-height: 26px;">&lt;/<span class="hljs-name" style="color: #f92672; line-height: 26px;">router-view</span>&gt;</span></span><br><span class="xml" style="line-height: 26px;"><span class="hljs-tag" style="color: #f92672; line-height: 26px;">&lt;/<span class="hljs-name" style="color: #f92672; line-height: 26px;">transition</span>&gt;</span></span><font></font><br><font></font><br><font></font><br><font></font><br><span class="hljs-number" style="line-height: 26px;">2.</span> 实现不同的过渡特效 ,组件内使用 &lt;transition&gt; 并设置不同的 name。<font></font><br> &lt;transition name=<span class="hljs-string" style="color: #a6e22e; line-height: 26px;">"long"</span>&gt;<br> <span class="xml" style="line-height: 26px;"><span class="hljs-tag" style="color: #f92672; line-height: 26px;">&lt;<span class="hljs-name" style="color: #f92672; line-height: 26px;">div</span> <span class="hljs-attr" style="line-height: 26px;">class</span>=<span class="hljs-string" style="color: #a6e22e; line-height: 26px;">"main"</span>&gt;</span>...<span class="hljs-tag" style="color: #f92672; line-height: 26px;">&lt;/<span class="hljs-name" style="color: #f92672; line-height: 26px;">div</span>&gt;</span></span><br> <span class="xml" style="line-height: 26px;"><span class="hljs-tag" style="color: #f92672; line-height: 26px;">&lt;/<span class="hljs-name" style="color: #f92672; line-height: 26px;">transition</span>&gt;</span></span><br></code></pre> <h4 data-tool="mdnice编辑器" style="margin-top: 30px; margin-bottom: 15px; font-weight: bold; color: black; font-size: 18px;"><span class="prefix" style="display: none;"></span><span class="content"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">路由懒加载</font></font></span><span class="suffix" style="display: none;"></span></h4> <blockquote data-tool="mdnice编辑器" style="font-size: 0.9em; overflow: auto; overflow-scrolling: touch; background: rgba(0, 0, 0, 0.05); color: #6a737d; padding-top: 10px; padding-bottom: 10px; padding-left: 20px; padding-right: 10px; margin-bottom: 20px; margin-top: 20px; padding: 15px 20px; line-height: 27px; background-color: rgb(239, 239, 239); border-left: none; display: block;"> <p style="padding-bottom: 8px; padding-top: 23px; margin: 0px; line-height: 26px; padding: 0px; font-size: 15px; color: rgb(89,89,89);"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">官方:当打包发展应用时,JavaScript包会变得非常大,影响页面加载。如果我们能把不同路由对应的组件分割成不同的代码块,然后当路由被访问的时候才加载对应组件,这样就更加高效了。</font></font></p> </blockquote> <pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px;"><code class="hljs" style="overflow-x: auto; padding: 16px; background: #272822; color: #ddd; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; border-radius: 0px; font-size: 12px; -webkit-overflow-scrolling: touch;"><span class="hljs-number" style="line-height: 26px;">1.</span> 所有路由都放置在一个同步块中 .routerPath.js 中<font></font><br><font></font><br><span class="hljs-keyword" style="color: #f92672; font-weight: bold; line-height: 26px;">const</span> Foo = <span class="hljs-function" style="line-height: 26px;"><span class="hljs-params" style="line-height: 26px;">()</span> =&gt;</span> <span class="hljs-keyword" style="color: #f92672; font-weight: bold; line-height: 26px;">import</span>(<span class="hljs-comment" style="color: #75715e; line-height: 26px;">/* webpackChunkName: "group-foo" */</span> <span class="hljs-string" style="color: #a6e22e; line-height: 26px;">'./Foo.vue'</span>)<br><span class="hljs-keyword" style="color: #f92672; font-weight: bold; line-height: 26px;">const</span> Bar = <span class="hljs-function" style="line-height: 26px;"><span class="hljs-params" style="line-height: 26px;">()</span> =&gt;</span> <span class="hljs-keyword" style="color: #f92672; font-weight: bold; line-height: 26px;">import</span>(<span class="hljs-comment" style="color: #75715e; line-height: 26px;">/* webpackChunkName: "group-foo" */</span> <span class="hljs-string" style="color: #a6e22e; line-height: 26px;">'./Bar.vue'</span>)<br><span class="hljs-keyword" style="color: #f92672; font-weight: bold; line-height: 26px;">const</span> Baz = <span class="hljs-function" style="line-height: 26px;"><span class="hljs-params" style="line-height: 26px;">()</span> =&gt;</span> <span class="hljs-keyword" style="color: #f92672; font-weight: bold; line-height: 26px;">import</span>(<span class="hljs-comment" style="color: #75715e; line-height: 26px;">/* webpackChunkName: "group-foo" */</span> <span class="hljs-string" style="color: #a6e22e; line-height: 26px;">'./Baz.vue'</span>)<font></font><br><font></font><br><font></font><br><span class="hljs-number" style="line-height: 26px;">2.</span> 在 router 文件中正常使用就可,使用 chunk name 来赋值 component<br><span class="hljs-keyword" style="color: #f92672; font-weight: bold; line-height: 26px;">const</span> router = <span class="hljs-keyword" style="color: #f92672; font-weight: bold; line-height: 26px;">new</span> VueRouter({<br> <span class="hljs-attr" style="line-height: 26px;">routes</span>: [<font></font><br> { <span class="hljs-attr" style="line-height: 26px;">path</span>: <span class="hljs-string" style="color: #a6e22e; line-height: 26px;">'/foo'</span>, <span class="hljs-attr" style="line-height: 26px;">component</span>: Foo },<font></font><br> { <span class="hljs-attr" style="line-height: 26px;">path</span>: <span class="hljs-string" style="color: #a6e22e; line-height: 26px;">'/bar'</span>, <span class="hljs-attr" style="line-height: 26px;">component</span>: Bar },<font></font><br> { <span class="hljs-attr" style="line-height: 26px;">path</span>: <span class="hljs-string" style="color: #a6e22e; line-height: 26px;">'/baz'</span>, <span class="hljs-attr" style="line-height: 26px;">component</span>: Baz }<font></font><br> ]<font></font><br>})<font></font><br><font></font><br></code></pre> </section> <br/> <br/> <center><h4>✨觉得不错的点赞,帮忙转发分享以下,原创不易!< <center><h3>🌟关注微信公众号'前端自学社区' 获取更多资料✨<h3/><center/>

<center><h3> 💥回复加群 可以加入 自学前端群💥
点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

MySQL部分从库上面因为大量的临时表tmp_table造成慢查询

背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_

皕杰报表之UUID

​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

2020年前端实用代码段,为你的工作保驾护航

有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )