前端框架Vue(2)——Vue-Router 路由跳转
2017-03-30 18:19
981 查看
1、最简单:html+js全在一个文件中
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>动态路由匹配</title> <script src="https://unpkg.com/vue/dist/vue.js"></script> <script src="https://unpkg.com/vue-router/dist/vue-router.js"></script> </head> <body> <div id="app"> <h1>Hello App!</h1> <p> <!-- 使用router-link 组件来导航 --> <!-- 通过传入 'to' 属性指定链接 --> <!-- <router-link>默认会渲染成一个a标签 --> <router-link to="/foo">GO TO FOO</router-link> <router-link to="/bar">GO TO BAR</router-link> </p> <!-- 路由出口 --> <!-- 路由匹配到的组件将会被渲染到这里 --> <router-view></router-view> </div> <script> // 1、定义组件 const Foo = {template:'<div>foo</div>'}; const Bar = {template:'<div>bar</div>'}; // 2、定义路由 // 每一个路由映射一个组件 const routes = [ {path:'/foo',component:Foo}, {path:'/bar',component:Bar} ] // 3、创建router实例,然后传'routes'配置 const router = new VueRouter({ routes }); // 4、创建和挂载根实例 const app = new Vue({ router }).$mount('#app'); </script> </body> </html>
2、命名路由 用name属性链接地址
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>命名路由</title> <script src="https://unpkg.com/vue/dist/vue.js"></script> <script src="https://unpkg.com/vue-router/dist/vue-router.js"></script> </head> <body> <div id="app"> <h1>Hello App!</h1> <p> <!-- 使用router-link 组件来导航 --> <!-- 通过传入 'to' 属性指定链接 --> <!-- <router-link>默认会渲染成一个a标签 --> <router-link :to="{name:'user',params:{id:123}}">GO TO FOO</router-link> <router-link :to="{name:'user',params:{id:456}}">GO TO BAR</router-link> </p> <!-- 路由出口 --> <!-- 路由匹配到的组件将会被渲染到这里 --> <router-view></router-view> </div> <script> // 1、定义组件 const Foo = {template:'<div>foo</div>'}; const Bar = {template:'<div>bar</div>'}; const User = { template:'<div>User {{$route.params.id}}</div>', } // 2、定义路由 // 每一个路由映射一个组件 // 3、创建router实例,然后传'routes'配置 const router = new VueRouter({ routes:[ { path:'/user/:id', name:'user', component: User } ] }); // 4、创建和挂载根实例 const app = new Vue({ router }).$mount('#app'); </script> </body> </html>
3、嵌套路由
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>路由嵌套</title> <script src="https://unpkg.com/vue/dist/vue.js"></script> <script src="https://unpkg.com/vue-router/dist/vue-router.js"></script> </head> <body> <div id="app"> <h1>Hello App!</h1> <p> <!-- 使用router-link 组件来导航 --> <!-- 通过传入 'to' 属性指定链接 --> <!-- <router-link>默认会渲染成一个a标签 --> <router-link to="/user/foo/profile">GO TO FOO</router-link> <router-link to="/user/bar/posts">GO TO BAR</router-link> </p> <!-- 路由出口 --> <!-- 路由匹配到的组件将会被渲染到这里 --> <router-view></router-view> </div> <script> // 1、定义组件 /*const Foo = {template:'<div>foo</div>'}; const Bar = {template:'<div>bar</div>'};*/ const User = { template:'<div class="user">' + '<h2>User {{$route.params.id}}</h2>' + '<router-view></router-view>' + '</div>' } const UserProfile = { template:'<div>profile</div>' } const UserPosts = { template:'<div>posts</div>' } // 2、定义路由 // 每一个路由映射一个组件 // 3、创建router实例,然后传'routes'配置 const router = new VueRouter({ routes:[ {path:'/user/:id',component: User, children:[ { //当/user/:id/profile匹配成功 //UserProfile会被渲染在User的<router-view> path:'profile', component:UserProfile }, { path:'posts', component:UserPosts } ] } ] }); // 4、创建和挂载根实例 const app = new Vue({ router }).$mount('#app'); </script> </body> </html>
4、命名视图
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>命名视图</title> <script src="https://unpkg.com/vue/dist/vue.js"></script> <script src="https://unpkg.com/vue-router/dist/vue-router.js"></script> </head> <body> <div id="app"> <h1>Hello Views!</h1> <ul> <li> <router-link to="/">/</router-link> </li> <li> <router-link to="/other">/other</router-link> </li> </ul> <router-view class="view one"></router-view> <router-view class="view two" name="a"></router-view> <router-view class="view three" name="b"></router-view> </div> <script> const Header = {template:'<div>头部</div>'} const Section = {template:'<div>中间部分</div>'} const Footer = {template:'<div>尾部</div>'} const router = new VueRouter({ mode:'history', routes:[ { path:'/', //redirect:'/other',//重定向 components:{ default:Header, a:Section, b:Footer } }, { path:'/other', components:{ default:Footer, a:Section, b:Header } } ] }) new Vue({ router, el:'#app' }) </script> </body> </html>
*当使用 Vue-cli 时如何进行 vue-router 路由跳转
如果没有使用vue-cli开发环境,请移步
http://blog.csdn.net/docallen/article/details/68490256
项目目录结构:
1、npm 安装 vue-router
cnpm install vue-router --save
2、首先建上图结构中的3个组件,写点内容即可
3、配置 router.js 文件
import Vue from 'vue' import VueRouter from 'vue-router' import Hello from '../component/Hello.vue' import firstPage from '../pages/firstPage.vue' import secondPage from '../pages/secondPage.vue' Vue.use(VueRouter) export default new VueRouter({ routes:[ { path:'/', name:'Hello', component:Hello }, { path:'/first', name:'first', component:firstPage }, { path:'/second', name:'second', component:secondPage } ] })
4、修改 APP.vue 中的 template
<template> <div id="app"> <img src="./assets/logo.png"> <div class="nav-list"> <router-link class="nav-item" to="/">首页</router-link> <router-link class="nav-item" to="/first">页面一</router-link> <router-link class="nav-item" to="/second">页面二</router-link> </div> <div> <router-view></r cc21 outer-view> </div> </div> </template>
5、修改 main.js
引入router.js
import router from './router/router.js'
修改 Vue 实例
new Vue({ el: '#app', router, render: h => h(App) })
同时感谢 李洪威 的好文 附上链接 https://segmentfault.com/a/1190000008557578
相关文章推荐
- 前端路由 -- vue-router
- vue通过 vue.mixin(混合)实现 任意页面记录滚动条位置(路由router.query参数一致) 并跳转到上次的位置
- 前端框架vue.js系列(13):路由
- Vue-router路由判断页面未登录跳转到登录页面
- vue-router路由跳转判断用户是否存在
- 前端框架Aurelia —— 路由Router
- Vue-router路由判断页面未登录跳转到登录页面的实例
- 浅析前端路由简介以及vue-router实现原理
- vue-router 源码实现前端路由的两种方式
- VueJs路由跳转中vue-router的使用
- Vue 前端路由 vue-router
- vue2.0子路由配置和跳转 vue-router: 嵌套路由
- 在vue-cli脚手架中配置一个vue-router前端路由
- VueJs路由跳转——vue-router的使用
- Vue.js结合vue-router和webpack编写单页路由项目
- Vue.js结合vue-router和webpack编写单页路由项目
- 前端mvvm框架vuejs教程集合
- Vue.js搭建路由报错 router.map is not a function
- Vue.js 2.0 和 React、Augular等其他前端框架大比拼
- 在nodeJs的Express框架下用TypeScript编写router路由出现import关键字错误的解决方案