vue實(shí)現(xiàn)頁面跳轉(zhuǎn)和參數(shù)傳遞的兩種方式
目標(biāo):兩種方式,實(shí)現(xiàn)vue組件間跳轉(zhuǎn)和參數(shù)傳遞
一、路由方式
頁面跳轉(zhuǎn)
- 當(dāng)前組件使用$.router.push,通過參數(shù)name對(duì)應(yīng)路由中目標(biāo)組件的name實(shí)現(xiàn)跳轉(zhuǎn)
參數(shù)傳遞
- 傳值:當(dāng)前組件使用$.router.push,通過參數(shù)query對(duì)應(yīng)路由里目標(biāo)組件props中的route.query接
- 參:目標(biāo)組件script中使用$.router.query接收參數(shù),頁面中直接寫參數(shù)名
(方法不唯一,還有其他方式)
1. 路由
const router = new Router({
routes: [{
path: '/list',
name: 'List',
component: () => import('@/components/demo2/List.vue')
},{
path: '/detail',
name: 'Detail',
component: () => import('@/components/demo2/Detail.vue'),
props: route => ({param: route.query.param})
}]
})2. 列表頁面
<template>
<div>
<h1>列表頁面</h1>
<div>
<el-button type="primary" @click="toDetail">點(diǎn)擊跳轉(zhuǎn)詳情</el-button>
</div>
</div>
</template>
<script>
export default {
name: "List",
data() {
return {
myId: "123"
};
},
methods: {
toDetail() {
this.$router.push({
name: 'Detail',
query: {param: this.myId}
})
}
}
}
</script>3. 詳情頁面
<template>
<div>
<h1>詳情頁面</h1>
<div>
<el-button type="primary" @click="toList">點(diǎn)擊返回列表</el-button>
<div>傳遞參數(shù):{{myId}}</div>
</div>
</div>
</template>
<script>
export default {
name: "Detail",
data() {
return {
myId : this.$route.query.param
};
},
methods:{
toList(){
this.$router.push({
name: 'List',
})
}
}
}
</script>二、組件方式
只需配置一個(gè)路由即可實(shí)現(xiàn)不同頁面跳轉(zhuǎn),頁面跳轉(zhuǎn)和參數(shù)傳遞通過組件間調(diào)用實(shí)現(xiàn)
頁面跳轉(zhuǎn)
- 父組件 → 子組件
- 引用子組件,利用v-if標(biāo)簽分別選擇顯示對(duì)應(yīng)組件
- 子組件 → 父組件
- 子組件使用$.emit(事件)調(diào)用父組件方法改變自定義參數(shù)(show)實(shí)現(xiàn)跳轉(zhuǎn)
參數(shù)傳遞
- 父組件 → 子組件
- 傳值:父組件引用子組件標(biāo)簽(<my-detail :id="父組件參數(shù)"></my-detail>)中傳遞參數(shù)
- 接參:子組件接收參數(shù)使用props:['id']
- 子組件 → 父組件
- 傳值:子組件使用$.emit(父組件方法,參數(shù))傳遞參數(shù)
- 接參:父組件通過方法名(參數(shù))接收
1. 路由
const router = new Router({
routes: [{
path: '/main',
name: 'Main',
component: () => import('@/components/demo1/Main.vue')
}]
})2. 主頁組件
<template>
<div>
<h1>主頁面</h1>
<my-list v-if="show == 'list'" @toDetail="toDetail"></my-list>
<my-detail v-if="show == 'detail'" @toList="toList" :myId="myId"></my-detail>
</div>
</template>
<script>
import MyList from "@/components/demo1/MyList"
import MyDetail from "@/components/demo1/MyDetail"
export default {
name: "Main",
components: {
MyList,
MyDetail
},
data() {
return {
show: "list",
myId: ""
};
},
methods:{
toDetail(data){
this.show = "detail"
this.myId = data
},
toList(){
this.show = "list"
}
}
}
</script>3. 列表子組件
<template>
<div>
<h2>列表頁面</h2>
<div>
<el-button type="primary" @click="toDetail">點(diǎn)擊跳轉(zhuǎn)詳情</el-button>
</div>
</div>
</template>
<script>
export default {
name: "MyList",
data() {
return {
myId: "123"
};
},
methods: {
toDetail(data) {
this.$emit("toDetail",this.myId)
}
}
}
</script>4. 詳情子組件
<template>
<div>
<h2>詳情頁面</h2>
<div>
<el-button type="primary" @click="toList">點(diǎn)擊返回列表</el-button>
<div>傳遞參數(shù):{{myId}}</div>
</div>
</div>
</template>
<script>
export default {
name: "MyDetail",
props:['myId'],
data() {
return {
};
},
methods:{
toList(){
this.$emit("toList")
}
}
}
</script>到此這篇關(guān)于vue頁面跳轉(zhuǎn)和參數(shù)傳遞的文章就介紹到這了,更多相關(guān)vue頁面跳轉(zhuǎn)和參數(shù)傳遞內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- vue 倒計(jì)時(shí)結(jié)束跳轉(zhuǎn)頁面實(shí)現(xiàn)代碼
- vue如何跳轉(zhuǎn)到其他頁面
- vue跳轉(zhuǎn)頁面的幾種常用方法代碼示例
- vue 跳轉(zhuǎn)到其他頁面并關(guān)閉當(dāng)前頁面的實(shí)現(xiàn)代碼
- Vue路由跳轉(zhuǎn)傳參或打開新頁面跳轉(zhuǎn)的方法總結(jié)
- vue路由跳轉(zhuǎn)到新頁面實(shí)現(xiàn)置頂
- vue實(shí)現(xiàn)三級(jí)頁面跳轉(zhuǎn)功能
- vue3頁面跳轉(zhuǎn)的兩種方式
- vue3 setup點(diǎn)擊跳轉(zhuǎn)頁面的實(shí)現(xiàn)示例
相關(guān)文章
vue element-ui el-tooltip組件失效問題及解決
這篇文章主要介紹了vue element-ui el-tooltip組件失效問題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-10-10
Vue.js中extend選項(xiàng)和delimiters選項(xiàng)的比較
這篇文章主要介紹了Vue.js中extend選項(xiàng)和delimiters選項(xiàng)的比較的相關(guān)資料,需要的朋友可以參考下2017-07-07
websocket+Vuex實(shí)現(xiàn)一個(gè)實(shí)時(shí)聊天軟件
這篇文章主要利用websocked 建立長連接,利用Vuex全局通信的特性,以及watch,computed函數(shù)監(jiān)聽消息變化,并驅(qū)動(dòng)頁面變化實(shí)現(xiàn)實(shí)時(shí)聊天,感興趣的可以了解一下2021-08-08
vue實(shí)現(xiàn)數(shù)據(jù)控制視圖的原理解析
這篇文章主要介紹了vue如何實(shí)現(xiàn)的數(shù)據(jù)控制視圖的相關(guān)知識(shí),本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-01-01
ant?菜單組件報(bào)錯(cuò)Cannot?read?property?‘isRootMenu‘?of?undefin
這篇文章主要介紹了ant?菜單組件報(bào)錯(cuò)Cannot?read?property?‘isRootMenu‘?of?undefined解決,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-08-08
Vue中實(shí)現(xiàn)父子組件雙向數(shù)據(jù)流的三種方案分享
通常情況下,父子組件的通信都是單向的,或父組件使用props向子組件傳遞數(shù)據(jù),或子組件使用emit函數(shù)向父組件傳遞數(shù)據(jù),本文將嘗試講解Vue中常用的幾種雙向數(shù)據(jù)流的使用,需要的朋友可以參考下2023-08-08
關(guān)于Uncaught(in?promise)TypeError:?list?is?not?iterable報(bào)錯(cuò)
這篇文章主要給大家介紹了關(guān)于Uncaught(in?promise)TypeError:?list?is?not?iterable報(bào)錯(cuò)的解決方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-08-08

