导航
作用:前端路由
核心:锚点值的改变,根据不同的值,渲染指定DOM位置的不同数据
原理:
window.addEventListener('hashchange',fn);
<router-view></router-view><div id="xxx"></div>
作为一个DOM上的标识<div class="h">我是头部</div>
<div id="content" class="b"></div>
<div class="f">我是底部</div>
<script type="text/javascript">
//监视锚点值的改变
window.addEventListener('hashchange', function() {
var text = '';
switch (location.hash) {
case '#/music':
text = '各种音乐的数据';
break;
case '#/movie':
text = '各种电影的数据';
break;
}
document.getElementById('content').innerHTML = text;
})
</script>
经验:以后看到vue开头的插件,就知道必须Vue.use
Vue的核心插件:
使用方式:
npm i vue-router -S
import Vue from 'vue';
//配置路由
import VueRouter from 'vue-router';
Vue.use(VueRouter);
//1.创建组件
import Home from '../components/Home.vue';
import News from '../components/News.vue';
//2.配置路由 注意:名字
const routes = [
{ path: '/home', component: Home },
{ path: '/news', component: News, name: 'news' },
{ path: '*', redirect: '/home' } /*默认跳转路由*/
]
//3.实例化VueRouter 注意:名字
const router = new VueRouter({
mode: 'hash', /*hash模式改为history*/
routes // (缩写)相当于 routes: routes
})
//5 <router-view></router-view> 放在 App.vue
export default router;
import router from './router/router.js';
new Vue({
el: '#app',
router,
render: h => h(app);
});
<router-view></router-view>
let router = new VueRouter({
routes: [
{ name: 'home', path: '/home', component: Home },
{ name: 'music', path: '/music', component: Music },
{ path: '*', redirect: '/home' }
]
});
//1. 推荐写法,:to带name
// 更利于维护,如果只修改了路由配置中的path,该a标签会根据修改后的值生成href属性
<router-link :to="{name: 'home'}">首页</router-link>
<router-link :to="{name: 'music'}">音乐</router-link>
<router-view></router-view>
//2. 不推荐,直接写路径,不带冒号的to
<router-link to="/music">音乐</router-link>
在vue-router中,有两大对象被挂载到了实例 this
<router-link :to="{name:'detail',query:{id:1} } ">xxx</router-link>
{ name:'detail' , path:'/detail', component: Detail }
this.$route.query.id
<router-link :to="{name:'detail',params:{name:1} } ">xxx</router-link>
{ name:'detail' , path:'/detail/:name', component: Detail}
this.$route.params.name
作用:
使用:
{name:'xxx',query:{id:1},params:{name:2} }
goMoive() {
this.$router.push({
name: 'movie',
query: {id: 1, name: '古典'}
})
}
goNews() { this.$router.push({ path: '/content/495' }); }
.router-link-active {
color: red;
...
}
var router = new VueRouter({
routes: [...],
linkActiveClass: 'myactive'
});
{ path:'/', redirect:'/home' }
{ path:'/', redirect:{name:'home'} }
{ path:'*', component:notFoundVue}
main.js
//路由切换页面
import header from './components/header.vue'
import footer from './components/footer.vue'
//创建路由对象并配置路由规则
let router = new VueRouter({
routes: [{
path: '/',
components: {
header: header,
default: header,
footer: footer
}
}]
});
App.vue
// 一个组件中,放多个 router-view
<template>
<div>
<div>我是头部啊</div>
<hr/>
<!-- 留坑,非常重要 坑名-->
<router-view class="b" name="header"></router-view>
<router-view class="b" ></router-view>
<router-view class="b" name="footer"></router-view>
<hr/>
<div>我是底部啊</div>
</div>
</template>
注意:
// main.js
let router = new VueRouter({
routes: [
{ name: 'home', path: '/home', component: Home },
{ name: 'music', path: '/music', component: Music, // <====== 第一层路由
children: [ // <====== 第二层路由
{ name: 'oumei', path: 'oumei', component: Oumei },
{ name: 'guochan', path: 'guochan', component: Guochan }
]
},
{ path: '*', redirect: '/home' }
]
});
// App.vue
<div>
<sw-header></sw-header>
<router-view></router-view>
<sw-footer></sw-footer>
</div>
// Music.vue
<div>
<h3>我是音乐页</h3>
<router-link :to="{ name: 'oumei' }">欧美</router-link>
<router-link :to="{ name: 'guochan' }">国产</router-link>
<router-view></router-view>
</div>
// 一级页面
const Foo = {
template: `
<div>
<h2>Foo</h2>
<router-link to="/foo/bar">Go to Bar</router-link>
<router-view></router-view>
</div>
`
}
// 二级页面
const Bar = {
template: `
<div>Bar</div>
`
}
// 顶层出口
<template>
<div id="app">
<router-link to="/foo">Go to Foo</router-link>
<router-view></router-view>
</div>
</template>
const routes = [
{
path: '/foo',
component: Foo,
children: [
{
// 当 /foo/bar 匹配成功
// Bar 会被渲染在 Foo 组件的 <router-view> 中
path: 'bar',
component: Bar
}
]
}
]