0

I have the main Component using the Parent component having a dynamic component using the v-bind:is="componentName" just like:

Parent template:

<li class='list-inline-item g-mx-4 g-mt-10'>
  <component v-bind:is="componentName"></component>
</li>

and code:

 export default {

    data: function(){
             return  {
               componentName : "componentA"
             },
     },
    components: {
        componentA: {
            template : "<div>A</div>" 
        },
        componentB: {
          template : "  <div>B</div>"
        }
     }

I would like to switch the component to show sending a new value to componentName from a child component but I'm not sure if I have to use emit or watch or...?

I would like to do that without vuex.

2 Answers 2

1

Yes, use emit. In your parent component do something like :

<child-component @custom_event='methodName' />

And in a method of the child component :

this.$emit('custom_event')
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Loïc! I have update my script using emit and it works very well! Simple but very helpful!
1

I got solved it using emit as Loric suggested.

My new parent component is now:

   <li class='list-inline-item g-mx-4 g-mt-10'>
       <component v-bind:is="componentName"  @custom_event='updateTest'></component>
   </li>

and it has a mtehod like:

 methods: {
        updateTest() {
            const v = document.querySelector('meta[name="login-status"]').getAttribute("content");
            this.componentName =  (v!="") ? "componentLoggedOn" : "componentLoggedOff";
        },
    },

then I have add a new method to componentA component ( the one created inline) having:

this.$emit('custom_event', param);

Note: param is not used but just in case...

3 Comments

note that you can replace "custom_event" name with anything. For instance I have a component that is an ajax form, and it emits "submit" and "response". Also if you use v-bind you should use v-on to be consistent : Either : v-bind:is="componentName" v-on:custom_event='updateTest' or the short version : :is="componentName" @custom_event='updateTest'
Also more globally you are missing something big about vuejs. You shouldn't check the status of someone logged in like that. login_status could be a variable in your child-component, which is not optimal. It could also be a variable in vue-x. Or it could even be a global variable of your view instance.
Thanks @Loïc. I agree with you. I'm new on vue and I'm working on a blueprint project. I'll introduce Vuex in my project very soon.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.