vue跳转页面的几种常用方法实例总结
作者:战斗天使ZCY
Vue是一种流行的JavaScript框架,用于构建用户界面,在Vue中,页面跳转通常使用路由(Router)来实现,除此之外还有很多方法,这篇文章主要给大家介绍了关于vue跳转页面的几种常用方法,需要的朋友可以参考下
vue跳转不同页面的方法
1.router-link跳转
<!-- 直接跳转 -->
<router-link to='/testC'>
<button>点击跳转2</button>
</router-link>
<!-- 带参数跳转 -->
<router-link :to="{path:'testC',query:{setid:123456}}">
<button>点击跳转1</button>
</router-link>
<router-link :to="{name:'testC',params:{setid:1111222}}">
<button>点击跳转3</button>
</router-link>2.this.$router.push()
<template>
<div id='app'>
<button @click='goTo()'>点击跳转4</button>
</div>
</template>
<script>
new Vue({
el:'#app',
methods:{
goTo(){
//直接跳转
this.$router.push('/testDemo');
//带参数跳转
this.$router.push({path:'/testC',query:{setid:123456}});
this.$router.push({name:'testC',params:{setid:111222}});
}
}
})
</script>3.a标签可以跳转外部链接,不能路由跳转
<a href="https://www.baidu.com" rel="external nofollow" ><button>点击跳转5</button></a>
接收:this.$route.query.serid 和 this.$route.params.setid
vue三种不同方式实现跳转页面
Vue:router-link
<router-link to="/">[跳转到主页]</router-link>
<router-link to="/login">[登录]</router-link>
<router-link to="/logout">[登出]</router-link>
this.$router.push("/");this.$router.push("/")
<button @click="goHome">[跳转到主页]</button>
export default {
name: "App",
methods: {
// 跳转页面方法
goHome() {
this.$router.push("/");
},
}this.$router.go(1)
<button @click="upPage">[上一页]</button>
<button @click="downPage">[下一页]</button>
upPage() {
// 后退一步记录,等同于 history.back()
this.$router.go(-1);
},
downPage() {
// 在浏览器记录中前进一步,等同于 history.forward()
this.$router.go(1);
}代码示例:
<template>
<div id="app">
<img src="./assets/logo.png">
<router-view/>
<router-link to="/">[跳转到主页]</router-link>
<router-link to="/login">[登录]</router-link>
<router-link to="/logout">[登出]</router-link>
<!-- javascript跳转页面 -->
<button @click="goHome">[跳转到主页]</button>
<!-- 回到上一页 -->
<button @click="upPage">[上一页]</button>
<button @click="downPage">[下一页]</button>
<!-- 回到下一页 -->
</div>
</template>
<script>
export default {
name: "App",
methods: {
// 跳转页面方法
goHome() {
this.$router.push("/");
},
upPage() {
// 后退一步记录,等同于 history.back()
this.$router.go(-1);
},
downPage() {
// 在浏览器记录中前进一步,等同于 history.forward()
this.$router.go(1);
}
}
};
</script>总结
到此这篇关于vue跳转页面的几种常用方法的文章就介绍到这了,更多相关vue跳转页面方法内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
