本文章向大家介紹使用vue-router怎么實(shí)現(xiàn)一個(gè)組件間跳轉(zhuǎn)功能,主要包括使用vue-router怎么實(shí)現(xiàn)一個(gè)組件間跳轉(zhuǎn)功能的使用實(shí)例、應(yīng)用技巧、基本知識(shí)點(diǎn)總結(jié)和需要注意事項(xiàng),具有一定的參考價(jià)值,需要的朋友可以參考一下。

Vue具體輕量級(jí)框架、簡(jiǎn)單易學(xué)、雙向數(shù)據(jù)綁定、組件化、數(shù)據(jù)和結(jié)構(gòu)的分離、虛擬DOM、運(yùn)行速度快等優(yōu)勢(shì),Vue中頁(yè)面使用的是局部刷新,不用每次跳轉(zhuǎn)頁(yè)面都要請(qǐng)求所有數(shù)據(jù)和dom,可以大大提升訪(fǎng)問(wèn)速度和用戶(hù)體驗(yàn)。
login ---用戶(hù)名--->main
①明確發(fā)送方和接收方
②配置接收方的路由地址
{path:'/myTest',component:TestComponent}
-->
{path:'/myTest/:id',component:TestComponent}
③接收方獲取傳遞來(lái)的數(shù)據(jù)
this.$route.params.id
④跳轉(zhuǎn)的時(shí)候,發(fā)送參數(shù)
this.$router.push('/myTest/20')
<router-link :to="'/myTest'+id">跳轉(zhuǎn)</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>
</div>
<script>
//創(chuàng)建主頁(yè)面組件
var myMain = Vue.component("main-component",{
//保存登錄傳遞過(guò)來(lái)的數(shù)據(jù)
data:function(){
return {
uName:''
}
},
template:`
<div>
<h2>主頁(yè)面用戶(hù)名:{{uName}}</h2>
</div>
`,
//掛載該組件時(shí)自動(dòng)拿到數(shù)據(jù)
beforeMount:function(){
//接收參數(shù)
console.log(this.$route.params);
this.uName = this.$route.params.myName ;
}
})
//創(chuàng)建登錄頁(yè)面組件
var myLogin = Vue.component("login-component",{
//保存用戶(hù)輸入的數(shù)據(jù)
data:function(){
return {
userInput:""
}
},
methods:{
toMain:function(){
//跳轉(zhuǎn)到主頁(yè)面,并將用戶(hù)輸入的名字發(fā)送過(guò)去
this.$router.push("/main/"+this.userInput);
console.log(this.userInput);
}
},
template:`
<div>
<h2>登錄頁(yè)面</h2>
<input type="text" v-model="userInput" placeholder="請(qǐng)輸入用戶(hù)名">
<button @click="toMain">登錄到主頁(yè)面</button>
<br>
<router-link :to="'/main/'+userInput">登錄到主頁(yè)面</router-link>
</div>
`
})
var NotFound = Vue.component("not-found",{
template:`
<div>
<h2>404 Page Not Found</h2>
<router-link to="/login">返回登錄頁(yè)</router-link>
</div>
`
})
//配置路由詞典
const myRoutes = [
{path:"",component:myLogin},
{path:"/login",component:myLogin},
//注意冒號(hào),不用/否則會(huì)當(dāng)成地址
{path:"/main/:myName",component:myMain},
//沒(méi)有匹配到任何頁(yè)面則跳轉(zhuǎn)到notfound頁(yè)面
{path:"*",component:NotFound}
]
const myRouter = new VueRouter({
routes:myRoutes
})
new Vue({
router:myRouter,
el:"#container",
data:{
msg:"Hello VueJs"
}
})
// 注意,路由地址
</script>
</body>
</html><!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>傳參練習(xí)</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>
</div>
<script>
//創(chuàng)建產(chǎn)品列表組件
var myList = Vue.component("product-list",{
//保存產(chǎn)品列表的數(shù)據(jù)
data:function(){
return{
productList:["蘋(píng)果","華為","三星","小米","vivo"]
}
},
template:`
<div>
<h5>這是列表頁(yè)</h5>
<ul>
<li v-for="(tmp,index) in productList">
//將index傳遞過(guò)去
<router-link v-bind:to="'/detail/'+index">{{tmp}}</router-link>
</li>
</ul>
</div>
`
})
//詳情頁(yè)組件
var myDetail = Vue.component("product-detail",{
//保存?zhèn)鬟f過(guò)來(lái)的index
data:function(){
return{
myIndex:""
}
},
//在掛載完成后,將接收到的index賦值給myIndex
mounted:function(){
this.myIndex = this.$route.params.id;
},
template:`
<div>
<h5>這是詳情頁(yè)</h5>
<p>這是id為:{{myIndex}}的產(chǎn)品</p>
</div>
`
})
//頁(yè)面找不到的時(shí)候
var NotFound = Vue.component("not-found",{
template:`
<div>
<h2>404 Page Not Found</h2>
</div>
`
})
// 配置路由詞典
const myRoutes = [
{path:"",component:myList},
{path:"/list",component:myList},
{path:"/detail/:id",component:myDetail},
{path:"*",component:NotFound},
]
const myRouter = new VueRouter({
routes:myRoutes
})
new Vue({
router:myRouter,
el:"#container",
data:{
msg:"Hello VueJs"
}
})
</script>
</body>
</html>到此這篇關(guān)于使用vue-router怎么實(shí)現(xiàn)一個(gè)組件間跳轉(zhuǎn)功能的文章就介紹到這了,更多相關(guān)的內(nèi)容請(qǐng)搜索創(chuàng)新互聯(lián)成都網(wǎng)站設(shè)計(jì)公司以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持創(chuàng)新互聯(lián)成都網(wǎng)站設(shè)計(jì)公司!
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無(wú)理由+7*72小時(shí)售后在線(xiàn),公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性?xún)r(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專(zhuān)為企業(yè)上云打造定制,能夠滿(mǎn)足用戶(hù)豐富、多元化的應(yīng)用場(chǎng)景需求。
網(wǎng)站題目:使用vue-router怎么實(shí)現(xiàn)一個(gè)組件間跳轉(zhuǎn)功能-創(chuàng)新互聯(lián)
本文鏈接:http://chinadenli.net/article16/cophgg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供外貿(mào)建站、外貿(mào)網(wǎng)站建設(shè)、電子商務(wù)、軟件開(kāi)發(fā)、App開(kāi)發(fā)、做網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容