Vue-Router實(shí)現(xiàn)組件間跳轉(zhuǎn)的三種方法
通過(guò)VueRouter來(lái)實(shí)現(xiàn)組件之間的跳轉(zhuǎn),供大家參考,具體內(nèi)容如下
提供了3種方式實(shí)現(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>
<!--通過(guò)router-view指定盛放組件的容器 -->
<router-view></router-view>
</div>
<script>
var testLogin = Vue.component("login",{
template:`
<div>
<h1>這是我的登錄頁(yè)面</h1>
</div>
`
})
var testRegister = Vue.component("register",{
template:`
<div>
<h1>這是我的注冊(cè)頁(yè)面</h1>
</div>
`
})
//配置路由詞典
//對(duì)象數(shù)組
const myRoutes =[
//當(dāng)路由地址:地址欄中的那個(gè)路徑是myLogin訪問(wèn)組件
//組件是作為標(biāo)簽來(lái)用的所以不能直接在component后面使用
//要用返回值
//path:''指定地址欄為空:默認(rèn)為L(zhǎng)ogin頁(yè)面
{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>
②通過(guò)router-link實(shí)現(xiàn)跳轉(zhuǎn)
<router-link to="/myRegister">注冊(cè)</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>
<!--通過(guò)router-view指定盛放組件的容器 -->
<router-view></router-view>
</div>
<script>
var testLogin = Vue.component("login",{
template:`
<div>
<h1>這是我的登錄頁(yè)面</h1>
<router-link to="/myRegister">注冊(cè)</router-link>
</div>
`
/*to后面是路由地址*/
})
var testRegister = Vue.component("register",{
template:`
<div>
<h1>這是我的注冊(cè)頁(yè)面</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>
③通過(guò)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>
<!--通過(guò)router-view指定盛放組件的容器 -->
<router-view></router-view>
</div>
<script>
var testLogin = Vue.component("login",{
template:`
<div>
<h1>這是我的登錄頁(yè)面</h1>
<router-link to="/myRegister">注冊(cè)</router-link>
</div>
`
/*to后面是路由地址*/
})
var testRegister = Vue.component("register",{
methods:{
jumpToLogin:function(){
this.$router.push('/myLogin');
}
},
template:`
<div>
<h1>這是我的注冊(cè)頁(yè)面</h1>
<button @click="jumpToLogin">注冊(cè)完成,去登錄</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>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 使用Vue-router二級(jí)路由跳轉(zhuǎn)另一條路由下的子級(jí)
- vue-router嵌套路由方式(頁(yè)面路徑跳轉(zhuǎn)但頁(yè)面顯示空白)
- Vue3使用vue-router如何實(shí)現(xiàn)路由跳轉(zhuǎn)與參數(shù)獲取
- vue-router跳轉(zhuǎn)時(shí)打開(kāi)新頁(yè)面的兩種方法
- Vue-router路由判斷頁(yè)面未登錄跳轉(zhuǎn)到登錄頁(yè)面的實(shí)例
- Vue.js實(shí)戰(zhàn)之利用vue-router實(shí)現(xiàn)跳轉(zhuǎn)頁(yè)面
- vue-router跳轉(zhuǎn)頁(yè)面的方法
- vue-router跳轉(zhuǎn)方式的區(qū)別解析
相關(guān)文章
基于element-ui封裝可搜索的懶加載tree組件的實(shí)現(xiàn)
這篇文章主要介紹了基于element-ui封裝可搜索的懶加載tree組件的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-05-05
解決vue-cli項(xiàng)目打包出現(xiàn)空白頁(yè)和路徑錯(cuò)誤的問(wèn)題
今天小編就為大家分享一篇解決vue-cli項(xiàng)目打包出現(xiàn)空白頁(yè)和路徑錯(cuò)誤的問(wèn)題。具有很好的參考價(jià)值。希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-09-09
vue v-for循環(huán)出來(lái)的數(shù)據(jù)動(dòng)態(tài)綁定值問(wèn)題
這篇文章主要介紹了vue v-for循環(huán)出來(lái)的數(shù)據(jù)動(dòng)態(tài)綁定值問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-04-04
使用 vue 實(shí)現(xiàn)滅霸打響指英雄消失的效果附demo
這篇文章主要介紹了使用 vue 實(shí)現(xiàn)滅霸打響指英雄消失的效果 demo,需要的朋友可以參考下2019-05-05
vue-cli3項(xiàng)目升級(jí)到vue-cli4 的方法總結(jié)
這篇文章主要介紹了vue-cli3項(xiàng)目升級(jí)到vue-cli4 的方法總結(jié),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-03-03
使用vue-router設(shè)置每個(gè)頁(yè)面的title方法
下面小編就為大家分享一篇使用vue-router設(shè)置每個(gè)頁(yè)面的title方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-02-02

