Vuejs2 Modal with route in vue-router

2019-03-16 17:21发布

I'm trying to create a route with a modal and when you access with router-link to this router-path appears a modal above the current page or if i access directly from url appears the index with modal above.

For example: I'm in http://localhost/profile/1 and click in the sidebar Create team the url changes to http://localhost/team/create but the page behind the modal still is http://localhost/profile/1.

This is the code i'm trying:

router:

Vue.component('modal', Modal);    
export default new Router({
      mode: 'history',
      routes: [
       {
        path: '/',
        name: 'Hello',
        component: require('@/components/Hello'),
        meta: { auth: false }
      },

      {
        path: '/team',
        name: 'team',
        component: require('@/components/team/Index'),
        meta: { auth: true },
      },
      {
        path: '/team/create',
        name: 'CreateTeam',
        components: {
          b: CreateTeam
        },
        meta: { auth: true }
      },  
      ]
    })

App.vue

<template>

  <router-view></router-view>
  <!-- This is the "MODAL" router-view -->
  <router-view name="b"></router-view>              

</template>

Modal.vue

<template>
    <div class="modal">

      <slot name="body"></slot>  
        <button type="button" @click="$emit('close')">×</button>
   </div>
</template>

CreateTeam.vue

<template>
    <modal @close="vm.$router.go(-1)">

        <div slot="body" class="col-md-12">
        <!-- Form here -->
        </div>

    </modal>
</template>

Everything is working instead that when i go to /create/team behind the modal is empty

标签: vue.js
1条回答
2楼-- · 2019-03-16 17:55

If anyone needs this solution, i solved this way:

I created a component create-team Vue.component('create-team',CreateTeam) and i put it in the App.vue like this:

<create-team v-if="CreateTeam"></create-team>  

In the same App.vue i created a computed with a vuex getter:

computed: {

    CreateTeam() {
         return store.getters.createTeam
    }
  },

In Sidebar i created a link like this:

 <a @click="CreateTeam" class="user-panel-action-link" href="/create/team">
   <i class="fa fa-users" aria-hidden="true"></i> Crear Equipo
 </a>

And a method CreateTeam

CreateTeam(e) {
      e.preventDefault()
      store.commit('setTeamModal')
      history.pushState('', 'Title of page', '/create/team');

    }

The store.js vuex is simple:

const state = {

    createTeam: false,
}

const mutations = {

    setTeamModal (state) {
      state.createTeam= true
    },
    deleteTeamModal (state) {
      state.createTeam= false
    }
}

const actions = {
    setTeamModal: ({ commit }) => commit('setTeamModal')
   deleteTeamModal: ({ commit }) => commit('setTeamModal')
}

const getters = {

    createTeam: state => state.createTeam
}

export default new Vuex.Store({
    state,
    getters,
    actions,
    mutations
})

And the last thing is in the CreateTeam.vue component the Close method i made somthing like this:

 Close() {
      this.$store.commit('deleteTeamModal')
      this.$router.go(-1)
    }

  }

Maybe someone can make it better, that's my little piece of help

Greetings

查看更多
登录 后发表回答