Vue的route-view子頁(yè)面調(diào)用父頁(yè)面的函數(shù)詳解
route-view子頁(yè)面調(diào)用父頁(yè)面函數(shù)
最近寫項(xiàng)目的時(shí)候,有一個(gè)模塊需要刷新父頁(yè)面最新后臺(tái)數(shù)據(jù),然后再進(jìn)行操作,查詢很多資料搞不懂怎么調(diào)用的,現(xiàn)在解決了,做個(gè)記錄
vue版本為2.6
父頁(yè)面template代碼
<router-view ?v-on:getUser="getUser" :infoArray="infoArray"></router-view>
父頁(yè)面函數(shù)代碼
//data是子頁(yè)面?zhèn)鬟^(guò)來(lái)的參數(shù),如果不需要就不寫
getUser(data){
? ?this.infoArray= 123;
},子頁(yè)面的代碼
props: ['infoArray'],
inject:['getUser'],
created() {
? ?this.$emit("getUser","如果需要傳值,參數(shù)寫在這里");
},infoArray是父頁(yè)面查詢后獲取的結(jié)果,我這里子頁(yè)面有接收。
router-view解釋
在我們創(chuàng)建項(xiàng)目的時(shí)候,可以自然而然的發(fā)現(xiàn),那就是在app.vue這個(gè)頁(yè)面里面存在一個(gè)<router-view>的tag標(biāo)簽。通過(guò)它,我們點(diǎn)擊鏈接,即可顯示出Vue的HelloWorld.vue頁(yè)面。
那么,到底是什么讓HelloWorld.vue頁(yè)面顯示出來(lái),并且它的路由途徑和特點(diǎn)呢?
下面就一一來(lái)解釋一下

本質(zhì),RouterView【命令視圖】和RouterLink【命令路線】本身是兩個(gè)組件。
操作步驟分別為:
1、創(chuàng)建組件
2、命名路由
const router = new VueRouter({
routes: [
{
path: '/user/:userId',
name: 'user',
component: User
}
]
})3、使用
完整操作步驟代碼如下:
import Vue from 'vue'
import VueRouter from 'vue-router'
Vue.use(VueRouter)
const Home = { template: '<div>This is Home</div>' }
const Foo = { template: '<div>This is Foo</div>' }
const Bar = { template: '<div>This is Bar {{ $route.params.id }}</div>' }
const router = new VueRouter({
mode: 'history',
base: __dirname,
routes: [
{ path: '/', name: 'home', component: Home },
{ path: '/foo', name: 'foo', component: Foo },
{ path: '/bar/:id', name: 'bar', component: Bar }
]
})
new Vue({
router,
template: `
<div id="app">
<h1>Named Routes</h1>
<p>Current route name: {{ $route.name }}</p>
<ul>
<li><router-link :to="{ name: 'home' }">home</router-link></li>
<li><router-link :to="{ name: 'foo' }">foo</router-link></li>
<li><router-link :to="{ name: 'bar', params: { id: 123 }}">bar</router-link></li>
</ul>
<router-view class="view"></router-view>
</div>
`
}).$mount('#app')命令視圖:通常用在同時(shí)顯示多個(gè)視圖
<router-view class="view one"></router-view> <router-view class="view two" name="a"></router-view> <router-view class="view three" name="b"></router-view>
沒有name的視圖,將default作為其名稱,由于多個(gè)視圖的性質(zhì),因此多個(gè)視圖需要統(tǒng)一路徑的多個(gè)組件。
const router = new VueRouter({
routes: [
{
path: '/',
components: {
default: Foo,
a: Bar,
b: Baz
}
}
]
}) 嵌套命名:不同于非嵌套,它常常用于布局上

針對(duì)這種格局,我們通過(guò)對(duì)路由進(jìn)行配置來(lái)實(shí)現(xiàn)上述布局:
{
path: '/settings',
// You could also have named views at the top
component: UserSettings,
children: [{
path: 'emails',
component: UserEmailsSubscriptions
}, {
path: 'profile',
components: {
default: UserProfile,
helper: UserProfilePreview
}
}]
}而,其中對(duì)settings.profile的<template>部分設(shè)置如下:
<!-- UserSettings.vue --> <div> <h1>User Settings</h1> <NavBar/> <router-view/> <router-view name="helper"/> </div>
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Vue動(dòng)態(tài)擴(kuò)展表頭的表格及數(shù)據(jù)方式(數(shù)組嵌套對(duì)象)
這篇文章主要介紹了Vue動(dòng)態(tài)擴(kuò)展表頭的表格及數(shù)據(jù)方式(數(shù)組嵌套對(duì)象),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-03-03
vue 使用v-if切換輸入框時(shí)導(dǎo)致輸入框的數(shù)據(jù)內(nèi)容沒有清空的問(wèn)題解決(兩種解決方法)
這篇文章主要介紹了vue 使用v-if切換輸入框時(shí)導(dǎo)致輸入框的數(shù)據(jù)內(nèi)容沒有清空的問(wèn)題解決,本文給大家分享兩種解決方法,需要的朋友可以參考下2023-05-05
vue權(quán)限管理系統(tǒng)的實(shí)現(xiàn)代碼
這篇文章主要介紹了vue權(quán)限管理系統(tǒng)的實(shí)現(xiàn)代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-01-01
vue3實(shí)現(xiàn)按鈕權(quán)限管理的項(xiàng)目實(shí)踐
在做后臺(tái)管理系統(tǒng)時(shí),經(jīng)常會(huì)有權(quán)限管理的功能,本文主要介紹了vue3實(shí)現(xiàn)按鈕權(quán)限管理的項(xiàng)目實(shí)踐,具有一定的參考價(jià)值,感興趣的可以了解一下2023-08-08
關(guān)于Vue在ie10下空白頁(yè)的debug小結(jié)
這篇文章主要給大家介紹了關(guān)于Vue在ie10下空白頁(yè)的debug相關(guān)資料,這是最近在工作中遇到的一個(gè)問(wèn)題,通過(guò)查找相關(guān)的資料終于解決了,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2018-05-05

