App.vue页面
<template>
<div class="app-container">
<h1>App 根组件</h1>
<a href="#/home">首页</a>
<a href="#/movie">电影</a>
<a href="#/about">关于</a>
<hr />
<component :is="comName"></component>
</div>
</template>
<script>
// 导入组件
import Home from '@/components/Home.vue'
import Movie from '@/components/Movie.vue'
import About from '@/components/About.vue'
export default {
data() {
return {
// 在动态组件的位置,要展示组件的名字,值必须是字符串
comName: 'Home'
}
},
created() {
// 只要当前的APP组件一被创建,就立即监听window对象的onhashchange事件
window.onhashchange =() => {
console.log('监听到了hash地址的变化',location.hash);
switch(location.hash) {
case '#/home':
this.comName = 'Home';
break;
case '#/movie':
this.comName = 'Movie';
break;
case '#/about':
this.comName = 'About';
break;
}
}
},
name: 'App',
// 注册组件
components: {
Home,
Movie,
About
}
}
</script>
<style lang="less" scoped>
.app-container {
background-color: #efefef;
overflow: hidden;
margin: 10px;
padding: 15px;
> a {
margin-right: 10px;
}
}
</style>
About.vue页面
<template>
<div class="about-container">
<h3>About 组件</h3>
</div>
</template>
<script>
export default {
name: 'About'
}
</script>
<style lang="less" scoped>
.about-container {
min-height: 200px;
background-color: skyblue;
padding: 15px;
> a {
margin-right: 10px;
}
}
</style>
Home.vue页面
<template>
<div class="home-container">
<h3>Home 组件</h3>
</div>
</template>
<script>
export default {
name: 'Home'
}
</script>
<style lang="less" scoped>
.home-container {
min-height: 200px;
background-color: pink;
padding: 15px;
}
</style>
Movie.vue页面
<template>
<div class="movie-container">
<h3>Movie 组件</h3>
</div>
</template>
<script>
export default {
name: 'Movie'
}
</script>
<style lang="less" scoped>
.movie-container {
min-height: 200px;
background-color: lightsalmon;
padding: 15px;
}
</style>
App2.vue页面
<template>
<div class="app-container">
<h1>App2 组件</h1>
<!-- 当我们安装和配置了vue-router后,可以使用router-link代替a链接 -->
<router-link to="/home">首页</router-link>
<router-link to="/movie">电影</router-link>
<router-link to="/about">关于</router-link>
<!-- 这两种用一种即可 -->
<a href="#/home">首页</a>
<a href="#/movie">电影</a>
<a href="#/about">关于</a>
<hr />
<!-- 只要在项目中安装和配置了vue-router 就可以使用router-view这个组件 -->
<!-- 它的作用很单纯:占位符 -->
<router-view></router-view>
</div>
</template>
<script>
export default {
name: 'App'
}
</script>
<style lang="less" scoped>
.app-container {
background-color: #efefef;
overflow: hidden;
margin: 10px;
padding: 15px;
> a {
margin-right: 10px;
}
}
</style>
main.js页面
import Vue from 'vue'
// import App from './App.vue'
import App from './App2.vue'
// 导入路由模块,目的:拿到路由的实例对象
// 在进行模块化导入的时候,如果给定的是文件夹,则默认导入这个文件夹下,名字叫做index.js
import router from '@/router'
// 导入 bootstrap 样式
import 'bootstrap/dist/css/bootstrap.min.css'
// 全局样式
import '@/assets/global.css'
Vue.config.productionTip = false
new Vue({
render: h => h(App),
// 在Vue项目中,要想把路由使用起来,必须把路由实例对象,通过以下的方式进行挂载
// router:路由的实例对象
router
}).$mount('#app')
在src下面建一个router包,里面是index.js
// src/router/index.js是当前项目的路由模块
import Vue from 'vue'
import VueRouter from 'vue-router'
// 导入需要的组件
import Home from '@/components/Home.vue'
import Movie from '@/components/Movie.vue'
import About from '@/components/About.vue'
// 把VueRouter安装为Vue项目的插件
// Vue.use()函数的作用,就是用来安装插件的
Vue.use(VueRouter)
// 创建路由的实例对象
const router = new VueRouter({
// routes是一个数组,作用:定义hash地址与组件之间的对应关系
routes: [
// 重定向的路由规则
{ path: '/', redirect: '/home' },
// {path:'/home',component:要展示的组件}
{ path: '/home', component: Home },
{ path: '/movie', component: Movie },
{ path: '/about', component: About },
]
})
export default router
一样的效果
嵌套路由
About.vue
<template>
<div class="about-container">
<h3>About 组件</h3>
<hr />
<!-- 子级路由链接 -->
<router-link to="/about/tab1">tab1</router-link>
<router-link to="/about/tab2">tab2</router-link>
<hr />
<!-- 子级路由占位符 -->
<router-view></router-view>
</div>
</template>
<script>
export default {
name: 'About'
}
</script>
<style lang="less" scoped>
.about-container {
min-height: 200px;
background-color: skyblue;
padding: 15px;
> a {
margin-right: 10px;
}
}
</style>
index.js页面
// src/router/index.js是当前项目的路由模块
import Vue from 'vue'
import VueRouter from 'vue-router'
// 导入需要的组件
import Home from '@/components/Home.vue'
import Movie from '@/components/Movie.vue'
import About from '@/components/About.vue'
import Tab1 from '@/components/tabs/Tab1.vue'
import Tab2 from '@/components/tabs/Tab2.vue'
// 把VueRouter安装为Vue项目的插件
// Vue.use()函数的作用,就是用来安装插件的
Vue.use(VueRouter)
// 创建路由的实例对象
const router = new VueRouter({
// routes是一个数组,作用:定义hash地址与组件之间的对应关系
routes: [
// 重定向的路由规则
{ path: '/', redirect: '/home' },
// {path:'/home',component:要展示的组件}
{ path: '/home', component: Home },
{ path: '/movie', component: Movie },
{
path: '/about', component: About,
// 重定向到tab1页面
//redirect:'/about/tab1',
children: [
// 子路由规则
// 如果children数组中,某个路由path值为空字符串,则这条路由规则
// 子路由规则,叫做“默认子路由”
{ path: '', component: Tab1 },
//{ path: 'tab1', component: Tab1 },
{ path: 'tab2', component: Tab2 },
]
},
]
})
export default router
tabs下的tab1页面
<template>
<div class="tab1-container">
<h5>Tab1 组件</h5>
</div>
</template>
<script>
export default {
name: 'Tab1'
}
</script>
<style lang="less" scoped>
.tab1-container {
min-height: 150px;
background-color: greenyellow;
}
</style>
tab2页面
<template>
<div class="tab2-container">
<h5>Tab2 组件</h5>
</div>
</template>
<script>
export default {
name: 'Tab2'
}
</script>
<style lang="less" scoped>
.tab2-container {
min-height: 150px;
background-color: plum;
}
</style>