亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

router4如何在js中控制組件跳轉?

router4如何在js中控制組件跳轉?

繁花不似錦 2018-10-10 11:03:44
router4如何在js中控制組件跳轉
查看完整描述

1 回答

?
青春有我

TA貢獻1784條經驗 獲得超8個贊

<!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>

`

})

//配置路由詞典

//對象數組

const  myRoutes =[

//當路由地址:地址欄中的那個路徑是myLogin訪問組件

//組件是作為標簽來用的所以不能直接在component后面使用

//要用返回值

//path:''指定地址欄為空:默認為Login頁面

{path:'',component:testLogin},

{path:'/myLogin',component:testLogin},

{path:'/myRegister',component:testRegister}

]

const myRouter = new VueRouter({

//myRoutes可以直接用上面的數組替換

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實現跳轉

<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>



查看完整回答
反對 回復 2018-11-18
  • 1 回答
  • 0 關注
  • 858 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號