Vue-Router實現(xiàn)組件間跳轉(zhuǎn)的三種方法
更新時間:2017年11月07日 10:49:41 作者:匿名的girl
這篇文章主要為大家詳細(xì)介紹了Vue-Router來實現(xiàn)組件間跳轉(zhuǎn)的三種方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下
通過VueRouter來實現(xiàn)組件之間的跳轉(zhuǎn),供大家參考,具體內(nèi)容如下
提供了3種方式實現(xiàn)跳轉(zhuǎn):
①直接修改地址欄中的路由地址
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<script src="js/vue.js"></script>
<!-- 引入文件 -->
<script src="js/vue-router.js"></script>
</head>
<body>
<div id="container">
<p>{{msg}}</p>
<!--通過router-view指定盛放組件的容器 -->
<router-view></router-view>
</div>
<script>
var testLogin = Vue.component("login",{
template:`
<div>
<h1>這是我的登錄頁面</h1>
</div>
`
})
var testRegister = Vue.component("register",{
template:`
<div>
<h1>這是我的注冊頁面</h1>
</div>
`
})
//配置路由詞典
//對象數(shù)組
const myRoutes =[
//當(dāng)路由地址:地址欄中的那個路徑是myLogin訪問組件
//組件是作為標(biāo)簽來用的所以不能直接在component后面使用
//要用返回值
//path:''指定地址欄為空:默認(rèn)為Login頁面
{path:'',component:testLogin},
{path:'/myLogin',component:testLogin},
{path:'/myRegister',component:testRegister}
]
const myRouter = new VueRouter({
//myRoutes可以直接用上面的數(shù)組替換
routes:myRoutes
})
new Vue({
router:myRouter,
//或者:
/*
router:new VueRouter({
routes:[
{path:'/myLogin',component:testLogin},
{path:'/myRegister',component:testRegister}
]
})
*/
el:"#container",
data:{
msg:"Hello VueJs"
}
})
</script>
</body>
</html>
②通過router-link實現(xiàn)跳轉(zhuǎn)
<router-link to="/myRegister">注冊</router-link>
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<script src="js/vue.js"></script>
<!-- 引入文件 -->
<script src="js/vue-router.js"></script>
</head>
<body>
<div id="container">
<p>{{msg}}</p>
<!--通過router-view指定盛放組件的容器 -->
<router-view></router-view>
</div>
<script>
var testLogin = Vue.component("login",{
template:`
<div>
<h1>這是我的登錄頁面</h1>
<router-link to="/myRegister">注冊</router-link>
</div>
`
/*to后面是路由地址*/
})
var testRegister = Vue.component("register",{
template:`
<div>
<h1>這是我的注冊頁面</h1>
</div>
`
})
//配置路由詞典
const myRoutes =[
{path:'',component:testLogin},
{path:'/myLogin',component:testLogin},
{path:'/myRegister',component:testRegister}
]
const myRouter = new VueRouter({
routes:myRoutes
})
new Vue({
router:myRouter,
el:"#container",
data:{
msg:"Hello VueJs"
}
})
</script>
</body>
</html>
③通過js的編程的方式
jumpToLogin: function () {
this.$router.push('/myLogin');
}
代碼
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<script src="js/vue.js"></script>
<!-- 引入文件 -->
<script src="js/vue-router.js"></script>
</head>
<body>
<div id="container">
<p>{{msg}}</p>
<!--通過router-view指定盛放組件的容器 -->
<router-view></router-view>
</div>
<script>
var testLogin = Vue.component("login",{
template:`
<div>
<h1>這是我的登錄頁面</h1>
<router-link to="/myRegister">注冊</router-link>
</div>
`
/*to后面是路由地址*/
})
var testRegister = Vue.component("register",{
methods:{
jumpToLogin:function(){
this.$router.push('/myLogin');
}
},
template:`
<div>
<h1>這是我的注冊頁面</h1>
<button @click="jumpToLogin">注冊完成,去登錄</button>
</div>
`
})
//配置路由詞典
const myRoutes =[
{path:'',component:testLogin},
{path:'/myLogin',component:testLogin},
{path:'/myRegister',component:testRegister}
]
const myRouter = new VueRouter({
routes:myRoutes
})
new Vue({
router:myRouter,
el:"#container",
data:{
msg:"Hello VueJs"
}
})
</script>
</body>
</html>
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
vue.js中window.onresize的超詳細(xì)使用方法
這篇文章主要給大家介紹了關(guān)于vue.js中window.onresize的超詳細(xì)使用方法,window.onresize 是直接給window的onresize屬性綁定事件,只能有一個,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-12-12
vue2.0 中使用transition實現(xiàn)動畫效果使用心得
這篇文章主要介紹了vue2.0 中使用transition實現(xiàn)動畫效果使用心得,本文通過案例知識給大家介紹的非常詳細(xì),需要的朋友參考下吧2018-08-08
vue使用require.context實現(xiàn)動態(tài)注冊路由
這篇文章主要介紹了vue使用require.context實現(xiàn)動態(tài)注冊路由的方法,幫助大家更好的理解和使用vue框架,感興趣的朋友可以了解下2020-12-12

