Legendary

李洋的学习笔记


router.js

<pre><code>import Vue from 'vue' import Router from 'vue-router' import HelloWorld from '@/components/HelloWorld' import world1 from '@/components/world1' import world2 from '@/components/world2' Vue.use(Router) const routes = [ { // 动态路径参数 以冒号开头 path: '/hellowworld/:id', name: 'HelloWorld', component: HelloWorld, children:[ { // 以 / 开头的嵌套路径会被当作根路径。 path:'world1', //path:'/helloworld/:id/world1', component:world1 }, { path:'world2', component:world2 }, ] } ] const router = new Router({ routes }) export default router</code></pre> <h4>声明式导航:</h4> <pre><code>&lt;router-link :to="..."&gt; &lt;router-link :to="..." replace&gt;</code></pre> <h4>编程式导航:</h4> <pre><code>// 字符串 router.push('home') router.replace('home') // 对象 router.push({ path: 'home' }) // 命名的路由 router.push({ name: 'user', params: { userId: '123' }}) // 带查询参数,变成 /register?plan=private router.push({ path: 'register', query: { plan: 'private' }})</code></pre> <blockquote> <p><strong>如果提供了 path,params 会被忽略</strong></p> <pre><code>const userId = '123' router.push({ name: 'user', params: { userId }}) // -&gt; /user/123 router.push({ path: `/user/${userId}` }) // -&gt; /user/123 // 这里的 params 不生效 router.push({ path: '/user', params: { userId }}) // -&gt; /user</code></pre> </blockquote> <h4>router.go(n)</h4> <pre><code>// 在浏览器记录中前进一步,等同于 history.forward() router.go(1) // 后退一步记录,等同于 history.back() router.go(-1) // 前进 3 步记录 router.go(3) // 如果 history 记录不够用,那就默默地失败呗 router.go(-100) router.go(100)</code></pre>

页面列表

ITEM_HTML