vue init webpack vue-router-test
是否建立路由: 是css
在 component下建立 NavList 頁面vue
<template> <div> <div class="navlist"> <ul> <li><router-link :to="index">首頁</router-link></li> <li><router-link :to="course">課程</router-link></li> <li><router-link :to="master">專家</router-link></li> </ul> </div> </div> </template> <script> export default { name: "NavList", data() { return { index: "/", course: "/course", master: "/master", } } } </script> <style scoped> .navlist { width: 100%; height: 50px; background: #f1f1f1; } ul { list-style: none; } li { float: left; margin: 20px; } </style>
index.vue, master.vue, course.vue等, 僅展現 index.vuejava
<template> <div> <NavList/> 首頁 </div> </template> <script> import NavList from "./NavList"; export default { name: "index", components: {NavList}, data() { return { } } } </script> <style scoped> </style>
import Vue from 'vue' import Router from 'vue-router' import index from '@/components/index' import Course from '@/components/course' import Master from '@/components/master' import Java from '@/components/course/java' import Python from '@/components/course/python' Vue.use(Router) export default new Router({ routes: [ { path: '/', name: 'index', component: index }, { path: '/course', name: 'Course', component: Course, }, { path: '/master', name: "Master", component: Master } ] })
router-viewpython
<template> <div id="app"> <!--<img src="./assets/logo.png">--> <!--<NavList/>--> <router-view/> </div> </template>
有時候, 在二級頁面下還須要新的子頁面, 就須要使用路由嵌套功能webpack
<template> <div> java </div> </template> <script> export default { name: "java" } </script> <style scoped> </style>
使用 router-link 進行頁面跳轉web
添加 router-view 指定顯示區域vue-router
路徑導航使用全路徑npm
<template> <div> <NavList/> <div class="left"> <ul> <li><router-link :to="java">java</router-link></li> <li><router-link :to="python">python</router-link></li> <li>bigdata</li> </ul> </div> <div class="right"> 視圖區域 <router-view/> </div> </div> </template> <script> import NavList from "./NavList"; export default { name: "course", components: {NavList}, data() { return { java: "/course/java", python: "/course/python", } } } </script> <style scoped> .left, .right { float: left; } .left { margin-left: 0; } .right { margin-left: 50px; } </style>
使用 redirect 指定一開始進入的默認頁面app
{ path: '/course', name: 'Course', component: Course, // 默認進入重定向 redirect: "/course/java", // 子嵌套 children: [ { path: "java", name: "Java", component: Java }, { path: "python", name: "Python", component: Python } ] },
在vue中, 能夠經過參數傳遞, 將值或者對象傳遞給路由子頁面spa
data() { return { index: "/", course: "/course", master: "/master", obj: { name: "wenbronk", age: 18 } } }
<li><router-link :to="{ name: 'Master', params:{count: 100, type: obj}}">專家</router-link></li>
<template> <div> 專家: {{ $route.params.count }} - {{ $route.params.type.name }} </div> </template> <script> export default { name: "master" } </script> <style scoped> </style>
默認是 router-link-active, 更改
mode: "history", linkActiveClass: "active",
.active {
color: red
}
<li><router-link :to="index" exact>首頁</router-link></li>