0

In my vue/cli 4/vuex / bootstrap-vue project app I have big page of ads listings with modal dialogs defined and I want to move dialogs to separate *.vue file, as these dialogs would be used in other pages too To communicate when dialog must be opened I emit events, and these events are not triggered in my separate *.vue file. In src/views/ads/AdsListing.vue I define:

<template>

    <b-card class="frontend_listing_container">
        ...
        <ads-modal></ads-modal>
    </b-card>

</template>

<script>

    import Vue from 'vue'
    import VueResource from 'vue-resource'
    Vue.use(VueResource)

    import {bus} from '@/main'
    import appMixin from '@/appMixin';

    ...
    Vue.component('ads-modal', require('../ads/AdsModal.vue').default);

    export default {
        data() {

        ...
        methods: {
            runEditAdModal(ed_id) {
                console.log('runEditAdModal ed_id::')
                console.log(ed_id)  // IN THE CONSOLE I SEE THIS method is called

                console.log('+++ runEditAdModalbus::')
                console.log(bus) // AND VALID BUS OBJECT

                bus.$emit('showEditAdModal', ed_id)
            }

In src/views/ads/AdsModal.vue I try to catch event in component events :

<script>

    import Vue from 'vue'
    import VueResource from 'vue-resource'

    ...
    import {bus} from '@/main'
    import appMixin from '@/appMixin';

    ...

    export default {
        data() {
            return {
            ...
            }
        },

        name: 'adsModal',

        created() {
            alert( 'src/views/ads/AdsModal.vue CREATED::' ) // I DO NOT SEE THIS ALERT POPUP

        }, // created() {

        mounted() {
            alert( 'src/views/ads/AdsModal.vue MOUNTED::' )  // I DO NOT SEE THIS ALERT POPUP
            bus.$on('showEditAdModal', (ed_id) => {
                console.log('showEditAdModalged ed_id::')
                console.log(ed_id)
                ...
            }) // bus.$on('page_changed', (from, to) => {
        }, // mounted() {

As events are not triggered, showEditAdModal event is not cathced. What is wrong and how to fix it ?

"bootstrap-vue": "^2.1.0",
"font-awesome": "^4.7.0",
"moment": "^2.24.0",
"moment-timezone": "^0.5.27",
"vue": "^2.6.10",
"vue-date-pick": "^1.2.1",

UPDATED # 1: a) with

Vue.component('ads-modal', require('../ads/AdsModal.vue'));

I got warning :

vue.runtime.esm.js?2b0e:619 [Vue warn]: Failed to mount component: template or render function not defined.

found in

---> <AdsModal>
       <AdsListingPage>
         <HomePage> at src/views/Home.vue
           <AppContainer> at src/App.vue
             <Root>

But created/mounted events are not triggered;

b) with line

   Vue.component('ads-modal', () => import('../ads/AdsModal.vue'))

no warning and created/mounted events are not triggered anyway.

c) I tried to move import into src/main.js (commenting it in my src/views/ads/AdsListing.vue) and the same : created/mounted events are not triggered and I do not know how to trigger methods in AdsModal.vue.

mstdmstd
  • 2,195
  • 17
  • 63
  • 140
  • 1
    It maybe that your component is not registered correctly. Try changing your require to `require('../ads/AdsModal.vue')` (without the `.default`, unless you have other named exports in your `AdsModal.vue` file). Or switch to dynamic `import()`: `Vue.component('ads-modal', () => import('../ads/AdsModal.vue'))` – Troy Morehouse Feb 01 '20 at 18:17
  • Please look at UPDATED # 1 – mstdmstd Feb 02 '20 at 04:51
  • What about just importing the component is the normal way (no require): `import AdsModal from '../ads/AdsModal.vue'` and then register the component in your page/app `export default { ... components: { 'ads-modal': AdsModal }, ...}` – Troy Morehouse Feb 02 '20 at 05:49
  • I tried the way you proposed : the same result – mstdmstd Feb 02 '20 at 07:29
  • Does the child component render any markup? – Troy Morehouse Feb 02 '20 at 08:31
  • Yes, at top of this fiel I have – mstdmstd Feb 02 '20 at 08:49
  • I moved all modal definitions and js methods into main app container App.vue it it works ok. but that does not seem best decision. I am afraid that App.vue could be too big... – mstdmstd Feb 03 '20 at 07:06

0 Answers0