Answer the question
In order to leave comments, you need to log in
How to make it so that the modal is called from the desired component, and not from App.vue?
There is a modal component Modal.vue
// Modal.vue
<template>
<b-modal :class="className" v-model="modal" @ok="okModal" @cancel="okModal" :ok-variant="okButton">
{{modalContent}}
</b-modal>
</template>
<script>
import { bus } from '../../bus'
export default {
name: 'Modal',
props: {
className: {
type: String,
default: 'modal-primary'
},
modalContent: {
type: String,
default: ''
},
okButton: {
type: String,
default: 'primary'
}
},
data () {
return {
modal: false
}
},
methods: {
okModal () {
this.modal = false
this.$emit('click')
}
},
mounted () {
bus.$on('modalWindow', (val) => {
console.log('123val', val)
this.modal = val
})
},
beforeDestroy () {
bus.$off('modalWindow')
}
}
</script>
//Component.vue
</template>
//code
<Modal :modalContent="'Блок успешно сохранен'" @click="ok"/>
</b-form>
</template>
<script>
import Modal from './Modal'
import { bus } from '../../bus'
export default {
name: 'TextBlockForm',
components: {
Modal
},
props: {
success: {
type: Boolean,
default: false
}
},
watch: {
success (oldValue, newValue) {
if (oldValue) {
bus.$emit('modalWindow', true)
}
}
},
methods: {
sendData () {
///////code
if (this.success) {
bus.$emit('modalWindow', true)
}
},
ok () {
bus.$emit('modalWindow', false)
this.$emit('clickOnOk')
}
}
}
}
</script>
//api конфиг файл
axios.interceptors.response.use((response) => {
return response
}, (error) => {
if (error.response.status == 403) {
bus.$emit('modalWindow', true)
}
return Promise.reject(error)
})
//App.vue
<template>
<div>
<router-view></router-view>
<Modal :className="'modal-danger'" :modalContent="'Доступ запрещен'" :okButton="'danger'" @click="okModal"/>
</div>
</template>
<script>
import { bus } from '../bus'
import Modal from '../src/components/Modal'
export default {
name: 'app',
components: {Modal},
methods: {
okModal () {
bus.$emit('modalWindow', false)
this.$router.go(-1)
}
}
}
</script>
Answer the question
In order to leave comments, you need to log in
Xs, how to do local events using the global bus. It seems to me that the approach is wrong.
Alternatively, you can add a name to the modal via ref
add open method
// Modal.vue
...
methods: {
okModal () {
this.modal = false
this.$emit('click')
},
open (val) {
this.modal = val
},
},
...
this.$refs.yourModalNameRef.open();
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question