除了使用 <router-link>
创建 a 标签来定义导航链接,我们还可以借助 router 的实例方法,通过编写代码来实现。
想要导航到不同的 URL,则使用 router.push
方法。这个方法会向 history 栈添加一个新的记录,所以,当用户点击浏览器后退按钮时,则回到之前的 URL。
-
router.push
// 字符串
router.push('home')
// 对象
router.push({ path: 'home' })
// 命名的路由
router.push({ name: 'user', params: { userId: '123' }}) //
// 带查询参数,变成 /register?plan=private
router.push({ path: 'register', query: { plan: 'private' }})
router.replace
// 字符串
router.replace('home')
// 对象
router.replace({ path: 'home' })
// 命名的路由
router.replace({ name: 'user', params: { userId: '123' }})
// 带查询参数,变成 /register?plan=private
router.replace({ path: 'register', query: { plan: 'private' }})
# 跟 router.push 很像,唯一的不同就是,它不会向 history 添加新记录,而是跟它的方法名一样 —— 替换掉当前的 history 记录。
router.go 这个方法的参数是一个整数,意思是在 history 记录中向前或者后退多少步
// 在浏览器记录中前进一步,等同于 history.forward()
router.go(1)
// 后退一步记录,等同于 history.back()
router.go(-1)
// 前进 3 步记录
router.go(3)
// 如果 history 记录不够用,那就默默地失败呗
router.go(-100)
router.go(100)
一、案例登录跳转(含顶部)
访问地址自己拼接一下
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
body {
margin: 0;
}
.header {
height: 48px;
background-color: #499ef3;
line-height: 48px;
}
.header a {
color: white;
text-decoration: none;
padding: 0 10px;
}
</style>
<script src="./js/vue.min.js"></script>
<script src="./js/vue-router.js"></script>
</head>
<body>
<div id="app">
<div class="header">
<router-link to="/">Logo</router-link>
<router-link to="/home">首页</router-link>
<router-link to="/task">任务宝</router-link>
<router-link to="/message">消息宝</router-link>
<div style="float: right;">
<router-link to="/login">登录</router-link>
</div>
</div>
<div class="body">
<router-view></router-view>
</div>
</div>
<script>
const Home = {
data: function () {
return {
title: "欢迎使用xx系统"
}
},
template: `<h2>{{title}}</h2>`
};
const Task = {
data: function () {
return {}
},
template: `
<div>
<h2>任务宝页面</h2>
</div>`
};
const Message = {
data: function () {
return {}
},
template: `
<div>
<h2>消息宝页面</h2>
</div>`
};
const Login = {
data: function () {
return {
user: '',
pwd: ''
}
},
methods: {
doLogin: function () {
if (this.user.length > 0 && this.pwd.length > 0) {
this.$router.push({name: 'Task'});
// this.$router.replace({name: 'Task'});
}
}
},
template: `
<div style="width: 500px;margin: 100px auto">
<input type="text" placeholder="用户名" v-model="user"/>
<input type="text" placeholder="密码" v-model="pwd" />
<input type="button" value="提 交" @click="doLogin" />
</div>
`
};
const router = new VueRouter({
routes: [
{path: '/', component: Home},
{path: '/home', component: Home},
{path: '/login', component: Login, name: 'Login'},
{path: '/task', component: Task, name: 'Task'},
{path: '/message', component: Message, name: 'Message'}
]
})
var app = new Vue({
el: '#app',
data: {},
methods: {},
router: router
})
</script>
</body>
</html>
二、案例登录跳转(不含顶部)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
body {
margin: 0;
}
.header {
height: 48px;
background-color: #499ef3;
line-height: 48px;
}
.header a {
color: white;
text-decoration: none;
padding: 0 10px;
}
</style>
<script src="./js/vue.min.js"></script>
<script src="./js/vue-router.js"></script>
</head>
<body>
<div id="app">
<router-view></router-view>
</div>
<script>
const Home = {
data: function () {
return {
title: "欢迎使用xx系统"
}
},
template: `
<div>
<div class="header">
<router-link to="/">Logo</router-link>
<router-link to="/home">首页</router-link>
<router-link :to="{name:'Task'}">任务宝</router-link>
<router-link :to="{name:'Message'}">消息宝</router-link>
<div style="float: right;">
<router-link to="/login">登录</router-link>
</div>
</div>
<div class="body">
<router-view></router-view>
</div>
</div>
`
};
const Index = {template: '<h3>这是个首页呀...</h3>'}
const Task = {
data: function () {
return {}
},
template: `
<div>
<div class="left-menu">
<div class="head">任务宝</div>
<router-link :to="{name:'Fans'}">粉丝</router-link>
<router-link :to="{name:'Spread'}">推广码</router-link>
<router-link :to="{name:'Statistics'}">数据统计</router-link>
</div>
<div class="right-body">
<router-view></router-view>
</div>
</div>`
};
const Fans = {template: `<h3>粉丝页面</h3>`};
const Spread = {template: `<h3>推广码页面</h3>`};
const Statistics = {template: `<h3>数据统计页面</h3>`};
const Message = {
data: function () {
return {}
},
template: `
<div>
<div class="left-menu">
<div class="head">消息宝</div>
<router-link :to="{name:'Sop'}">SOP</router-link>
<router-link :to="{name:'Send'}">推送管理</router-link>
</div>
<div class="right-body">
<router-view></router-view>
</div>
</div>`
};
const Sop = {template: `<h3>SOP页面</h3>`};
const Send = {template: `<h3>推送管理页面</h3>`};
const Login = {
data: function () {
return {
user: '',
pwd: ''
}
},
methods: {
doLogin: function () {
if (this.user.length > 0 && this.pwd.length > 0) {
this.$router.push({name: 'Index'});
// this.$router.replace({name: 'Task'});
}
}
},
template: `
<div style="width: 500px;margin: 100px auto">
<input type="text" placeholder="用户名" v-model="user"/>
<input type="text" placeholder="密码" v-model="pwd" />
<input type="button" value="提 交" @click="doLogin" />
</div>
`
};
const router = new VueRouter({
routes: [
{
path: '/',
// component: Home,
redirect: '/login'
},
{path: '/login', component: Login, name: 'Login'},
{
path: '/home',
component: Home,
children: [
{
path: '',
component: Index,
name: "Index"
},
{
path: 'task',
component: Task,
name: 'Task',
children: [
{
path: 'fans',
component: Fans,
name: 'Fans'
},
{
path: 'spread',
component: Spread,
name: 'Spread'
},
{
path: 'statistics',
component: Statistics,
name: 'Statistics'
}
]
},
{
path: 'message',
component: Message,
name: 'Message',
children: [
{
path: 'sop',
component: Sop,
name: 'Sop'
},
{
path: 'send',
component: Send,
name: 'Send'
}
]
}
],
},
]
})
var app = new Vue({
el: '#app',
data: {},
methods: {},
router: router
})
</script>
</body>
</html>