Node.js/nuxt/传值:修订间差异
来自康健生活
| (未显示同一用户的2个中间版本) | |||
| 第1行: | 第1行: | ||
== 父传子 == | == 父传子 == | ||
<syntaxhighlight lang=" | eq: 使用 <code>:menus</code> 传递数据。 | ||
</syntaxhighlight> | 父组件 | ||
{{hc|pages/index.vue|<syntaxhighlight lang="html"> | |||
<template> | |||
<section class="main"> | |||
<ly-nav-menu :menus="LayoutNavMenu"></ly-nav-menu> | |||
... | |||
<script> | |||
import LayoutNavMenu from "../components/index/NavMenu"; | |||
export default { | |||
components: { | |||
'ly-nav-menu': LayoutNavMenu, | |||
}, | |||
data() { | |||
return { | |||
LayoutNavMenu: [], | |||
} | |||
}, | |||
asyncData({ params, query, $axios, error }) { | |||
return Promise.all([ | |||
$axios.get('/v2/body_parts/all'), | |||
]).then(([ LayoutNavMenu]) => { | |||
return { | |||
LayoutNavMenu: LayoutNavMenu.data.list, | |||
... | |||
} | |||
</script> | |||
</syntaxhighlight>}} | |||
子组件 | |||
{{hc|../components/index/NavMenu.vue|<syntaxhighlight lang="html5"> | |||
<template> | |||
... | |||
<template v-bind:index="item.id" | |||
v-for="item in Body_parts_all"> | |||
<el-submenu :index="item.id+''" | |||
:popper-append-to-body="false"> | |||
... | |||
</template> | |||
<script> | |||
... | |||
export default { | |||
name: 'ly-nav-menu', | |||
props: { | |||
menus: { | |||
type: Array, | |||
defaultValue: [] | |||
} | |||
}, | |||
... | |||
computed: { | |||
Body_parts_all() { | |||
let BPA = this.menus.map((item, index) => { | |||
... | |||
stopPropagation(e) { | |||
e = e || window.event | |||
if (e.stopPropagation) { //W3C阻止冒泡方法 | |||
e.stopPropagation() | |||
} else { | |||
e.cancelBubble = true //IE阻止冒泡方法 | |||
... | |||
</syntaxhighlight>}} | |||
== 兄弟传值 == | == 兄弟传值 == | ||
现在有一个需求是路由跳转页面,然后刷新数据,但是路由地址不变,变的是路由地址后面 query 的参数。 | 现在有一个需求是路由跳转页面,然后刷新数据,但是路由地址不变,变的是路由地址后面 query 的参数。 | ||
2020年7月26日 (日) 17:21的最新版本
父传子
eq: 使用 :menus 传递数据。
父组件
pages/index.vue
<template>
<section class="main">
<ly-nav-menu :menus="LayoutNavMenu"></ly-nav-menu>
...
<script>
import LayoutNavMenu from "../components/index/NavMenu";
export default {
components: {
'ly-nav-menu': LayoutNavMenu,
},
data() {
return {
LayoutNavMenu: [],
}
},
asyncData({ params, query, $axios, error }) {
return Promise.all([
$axios.get('/v2/body_parts/all'),
]).then(([ LayoutNavMenu]) => {
return {
LayoutNavMenu: LayoutNavMenu.data.list,
...
}
</script>
子组件
../components/index/NavMenu.vue
<template>
...
<template v-bind:index="item.id"
v-for="item in Body_parts_all">
<el-submenu :index="item.id+''"
:popper-append-to-body="false">
...
</template>
<script>
...
export default {
name: 'ly-nav-menu',
props: {
menus: {
type: Array,
defaultValue: []
}
},
...
computed: {
Body_parts_all() {
let BPA = this.menus.map((item, index) => {
...
stopPropagation(e) {
e = e || window.event
if (e.stopPropagation) { //W3C阻止冒泡方法
e.stopPropagation()
} else {
e.cancelBubble = true //IE阻止冒泡方法
...
兄弟传值
现在有一个需求是路由跳转页面,然后刷新数据,但是路由地址不变,变的是路由地址后面 query 的参数。
http://localhost:3000/search?pageView=SearchAll
子传父,父传子
navmenu -> index -> list -> listleft
父亲:index.vue 儿子:list.vue list 的儿子,父亲的孙子:listLeft.vue 儿子:navmenu.vue
index.vue
<NavMenu @searchPageViewValue="pageViewValue"/>
<List :searchPageViewValue="searchPageViewValue"/>
data () {
return {
searchPageViewValue: 'searchAll'
}
},
components: {
NavMenu,
List
},
props: {
pageView: {
type: String,
default: 'SearchAll'
},
list.vue
<ListLeft :searchPageViewValue="searchPageViewValue"/>
<listRight/>
props: {
searchPageViewValue: {
type: String,
default: 'searchAll'
}
},
listLeft.vue
<ListLeftArticle v-if="searchPageType === 'SearchAll' || searchPageType === 'SearchQa'"/>
<ListLeftFile v-if="searchPageType === 'SearchAll'"/>
<ListLeftUser v-if="searchPageType === 'SearchAll' || searchPageType === 'SearchUser'"/>
<ListPagination/>
navmenu.vue
<nuxt-link
:key="$route.path+'?pageView=SearchAll'"
:to="{path:'/search',query:{pageView:'SearchAll'}}"
class="tags-li-title is-active"
>综合 (24)
</nuxt-link>
watch: {
$route (to, from) {
this.$emit('searchPageViewValue', this.pageView)
}
},
路由
第二种方法是直接监听路由
listLeft.vue
<ListLeftArticle v-if="searchPageType === 'SearchAll' || searchPageType === 'SearchQa'"/>
<ListLeftFile v-if="searchPageType === 'SearchAll'"/>
<ListLeftUser v-if="searchPageType === 'SearchAll' || searchPageType === 'SearchUser'"/>
<ListPagination/>
watch: {
$route (to, from) {
this.searchPageType = this.$route.query.pageView
}
},
事件车
eventBus.js
import Vue from 'vue'
export default new Vue();
AllCountry.vue
import EventBus from "@/router/eventBus";
mounted() {
this.allCountryEvent();
},
methods: {
renderData() {
...
allCountryEvent() {
EventBus.$emit('allCountry', {
isCountry: this.isCountry,
isDepartment: this.isDepartment,
isCountryValue: this.isCountryValue,
isDepartmentValue: this.isDepartmentValue,
})
}
HospitalList.vue
import EventBus from "@/router/eventBus";
methods: {
...
allCountryMessage() {
let _self = this;
EventBus.$on('allCountry', (message) => {
_self.allCountryCache = message;
_self.currentPage = 1;
_self.getData(
message.isCountry,
message.isCountryValue,
message.isDepartment,
message.isDepartmentValue,
_self.currentPage
)
})
}
No categories