Node.js/nuxt/传值

来自康健生活
Kangkk​(留言 | 贡献)2020年7月16日 (四) 05:28的版本 (→事件车)

兄弟传值

现在有一个需求是路由跳转页面,然后刷新数据,但是路由地址不变,变的是路由地址后面 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
                    )
                })
            }