vuejs - Async AND Dynamic component











up vote
0
down vote

favorite












What I want to achive is to combine vuejs's "Async Components" and its "Dynamic Components" (https://vuejs.org/v2/guide/components-dynamic-async.html) to get a flexible way of using dynamic components.



Let's say, I have the following component:



<template>
<div>
some stuff..
<component :is="type"></component>
some stuff...
</div>
</template>

<script>
export default {
name: "mycomponent",
props: {
type: {}
}
}
</script>


I would use it like so:



<mycomponent type="myinput"></mycomponent>


To make it work, I would need to load the myinput component in the mycomponent and I will do it aync:



components: {
MyInput: () => import("./myinput")
}


That works!



The problem now is, that I want to make it dynamic and use it like so:



<mycomponent type="myinput"></mycomponent>
<mycomponent type="myselect"></mycomponent>
<mycomponent type="mytextarea"></mycomponent>
...


To get this up and running, I would need to import all of this components in the mycomponent, which is obviously not an good idea. Global import is as well, not the way to go.



This was just an example, but the real use case for this is getting an array of type's from an api and dynamically render a form with it.










share|improve this question






















  • Why is it a bad idea to import them all? Only those components will be loaded which render is requested.
    – eldo
    Nov 7 at 12:59










  • Because then, the component needs to know, which other components exists, even if they are not used (in this request). And every time I (or someone else) create a new component, I need to add it to the mycomponent.
    – bernhardh
    Nov 7 at 14:26










  • Well that is true but even if you can leave it out here you will always have to import it somewhere. When you build your project all your lazy loaded components are added to the document as an async script. If you do not import these components somewhere you will not be able to load them.
    – eldo
    Nov 7 at 16:36















up vote
0
down vote

favorite












What I want to achive is to combine vuejs's "Async Components" and its "Dynamic Components" (https://vuejs.org/v2/guide/components-dynamic-async.html) to get a flexible way of using dynamic components.



Let's say, I have the following component:



<template>
<div>
some stuff..
<component :is="type"></component>
some stuff...
</div>
</template>

<script>
export default {
name: "mycomponent",
props: {
type: {}
}
}
</script>


I would use it like so:



<mycomponent type="myinput"></mycomponent>


To make it work, I would need to load the myinput component in the mycomponent and I will do it aync:



components: {
MyInput: () => import("./myinput")
}


That works!



The problem now is, that I want to make it dynamic and use it like so:



<mycomponent type="myinput"></mycomponent>
<mycomponent type="myselect"></mycomponent>
<mycomponent type="mytextarea"></mycomponent>
...


To get this up and running, I would need to import all of this components in the mycomponent, which is obviously not an good idea. Global import is as well, not the way to go.



This was just an example, but the real use case for this is getting an array of type's from an api and dynamically render a form with it.










share|improve this question






















  • Why is it a bad idea to import them all? Only those components will be loaded which render is requested.
    – eldo
    Nov 7 at 12:59










  • Because then, the component needs to know, which other components exists, even if they are not used (in this request). And every time I (or someone else) create a new component, I need to add it to the mycomponent.
    – bernhardh
    Nov 7 at 14:26










  • Well that is true but even if you can leave it out here you will always have to import it somewhere. When you build your project all your lazy loaded components are added to the document as an async script. If you do not import these components somewhere you will not be able to load them.
    – eldo
    Nov 7 at 16:36













up vote
0
down vote

favorite









up vote
0
down vote

favorite











What I want to achive is to combine vuejs's "Async Components" and its "Dynamic Components" (https://vuejs.org/v2/guide/components-dynamic-async.html) to get a flexible way of using dynamic components.



Let's say, I have the following component:



<template>
<div>
some stuff..
<component :is="type"></component>
some stuff...
</div>
</template>

<script>
export default {
name: "mycomponent",
props: {
type: {}
}
}
</script>


I would use it like so:



<mycomponent type="myinput"></mycomponent>


To make it work, I would need to load the myinput component in the mycomponent and I will do it aync:



components: {
MyInput: () => import("./myinput")
}


That works!



The problem now is, that I want to make it dynamic and use it like so:



<mycomponent type="myinput"></mycomponent>
<mycomponent type="myselect"></mycomponent>
<mycomponent type="mytextarea"></mycomponent>
...


To get this up and running, I would need to import all of this components in the mycomponent, which is obviously not an good idea. Global import is as well, not the way to go.



This was just an example, but the real use case for this is getting an array of type's from an api and dynamically render a form with it.










share|improve this question













What I want to achive is to combine vuejs's "Async Components" and its "Dynamic Components" (https://vuejs.org/v2/guide/components-dynamic-async.html) to get a flexible way of using dynamic components.



Let's say, I have the following component:



<template>
<div>
some stuff..
<component :is="type"></component>
some stuff...
</div>
</template>

<script>
export default {
name: "mycomponent",
props: {
type: {}
}
}
</script>


I would use it like so:



<mycomponent type="myinput"></mycomponent>


To make it work, I would need to load the myinput component in the mycomponent and I will do it aync:



components: {
MyInput: () => import("./myinput")
}


That works!



The problem now is, that I want to make it dynamic and use it like so:



<mycomponent type="myinput"></mycomponent>
<mycomponent type="myselect"></mycomponent>
<mycomponent type="mytextarea"></mycomponent>
...


To get this up and running, I would need to import all of this components in the mycomponent, which is obviously not an good idea. Global import is as well, not the way to go.



This was just an example, but the real use case for this is getting an array of type's from an api and dynamically render a form with it.







vuejs2






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 7 at 11:10









bernhardh

1,21652143




1,21652143












  • Why is it a bad idea to import them all? Only those components will be loaded which render is requested.
    – eldo
    Nov 7 at 12:59










  • Because then, the component needs to know, which other components exists, even if they are not used (in this request). And every time I (or someone else) create a new component, I need to add it to the mycomponent.
    – bernhardh
    Nov 7 at 14:26










  • Well that is true but even if you can leave it out here you will always have to import it somewhere. When you build your project all your lazy loaded components are added to the document as an async script. If you do not import these components somewhere you will not be able to load them.
    – eldo
    Nov 7 at 16:36


















  • Why is it a bad idea to import them all? Only those components will be loaded which render is requested.
    – eldo
    Nov 7 at 12:59










  • Because then, the component needs to know, which other components exists, even if they are not used (in this request). And every time I (or someone else) create a new component, I need to add it to the mycomponent.
    – bernhardh
    Nov 7 at 14:26










  • Well that is true but even if you can leave it out here you will always have to import it somewhere. When you build your project all your lazy loaded components are added to the document as an async script. If you do not import these components somewhere you will not be able to load them.
    – eldo
    Nov 7 at 16:36
















Why is it a bad idea to import them all? Only those components will be loaded which render is requested.
– eldo
Nov 7 at 12:59




Why is it a bad idea to import them all? Only those components will be loaded which render is requested.
– eldo
Nov 7 at 12:59












Because then, the component needs to know, which other components exists, even if they are not used (in this request). And every time I (or someone else) create a new component, I need to add it to the mycomponent.
– bernhardh
Nov 7 at 14:26




Because then, the component needs to know, which other components exists, even if they are not used (in this request). And every time I (or someone else) create a new component, I need to add it to the mycomponent.
– bernhardh
Nov 7 at 14:26












Well that is true but even if you can leave it out here you will always have to import it somewhere. When you build your project all your lazy loaded components are added to the document as an async script. If you do not import these components somewhere you will not be able to load them.
– eldo
Nov 7 at 16:36




Well that is true but even if you can leave it out here you will always have to import it somewhere. When you build your project all your lazy loaded components are added to the document as an async script. If you do not import these components somewhere you will not be able to load them.
– eldo
Nov 7 at 16:36












1 Answer
1






active

oldest

votes

















up vote
0
down vote



accepted










So I have came up with I idea what I could not try yet.



Lets say you have a component for different types of inputs for the sake of simplicity you name them all with an 'Input' prefix.



As long as everyone follows the naming rules you can register all of these component globally.



For example in a BaseInputs.js



const components = require.context(path, subfolder, regexForInputComponentFileNames)


https://webpack.js.org/guides/dependency-management/#require-context



components.keys().forEach(element => {
const componentName = element.replace(/*Everything that is not the component name like .vue*/)

Vue.component(componentName, () => import(path + componentName)
})


Then you just import 'path/to/BaseInputs' at the beginning of your application.



As I said I have not tried it yet it might wrong somewhere or missing something I will give it a try as soon as I can.






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














     

    draft saved


    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53188312%2fvuejs-async-and-dynamic-component%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote



    accepted










    So I have came up with I idea what I could not try yet.



    Lets say you have a component for different types of inputs for the sake of simplicity you name them all with an 'Input' prefix.



    As long as everyone follows the naming rules you can register all of these component globally.



    For example in a BaseInputs.js



    const components = require.context(path, subfolder, regexForInputComponentFileNames)


    https://webpack.js.org/guides/dependency-management/#require-context



    components.keys().forEach(element => {
    const componentName = element.replace(/*Everything that is not the component name like .vue*/)

    Vue.component(componentName, () => import(path + componentName)
    })


    Then you just import 'path/to/BaseInputs' at the beginning of your application.



    As I said I have not tried it yet it might wrong somewhere or missing something I will give it a try as soon as I can.






    share|improve this answer



























      up vote
      0
      down vote



      accepted










      So I have came up with I idea what I could not try yet.



      Lets say you have a component for different types of inputs for the sake of simplicity you name them all with an 'Input' prefix.



      As long as everyone follows the naming rules you can register all of these component globally.



      For example in a BaseInputs.js



      const components = require.context(path, subfolder, regexForInputComponentFileNames)


      https://webpack.js.org/guides/dependency-management/#require-context



      components.keys().forEach(element => {
      const componentName = element.replace(/*Everything that is not the component name like .vue*/)

      Vue.component(componentName, () => import(path + componentName)
      })


      Then you just import 'path/to/BaseInputs' at the beginning of your application.



      As I said I have not tried it yet it might wrong somewhere or missing something I will give it a try as soon as I can.






      share|improve this answer

























        up vote
        0
        down vote



        accepted







        up vote
        0
        down vote



        accepted






        So I have came up with I idea what I could not try yet.



        Lets say you have a component for different types of inputs for the sake of simplicity you name them all with an 'Input' prefix.



        As long as everyone follows the naming rules you can register all of these component globally.



        For example in a BaseInputs.js



        const components = require.context(path, subfolder, regexForInputComponentFileNames)


        https://webpack.js.org/guides/dependency-management/#require-context



        components.keys().forEach(element => {
        const componentName = element.replace(/*Everything that is not the component name like .vue*/)

        Vue.component(componentName, () => import(path + componentName)
        })


        Then you just import 'path/to/BaseInputs' at the beginning of your application.



        As I said I have not tried it yet it might wrong somewhere or missing something I will give it a try as soon as I can.






        share|improve this answer














        So I have came up with I idea what I could not try yet.



        Lets say you have a component for different types of inputs for the sake of simplicity you name them all with an 'Input' prefix.



        As long as everyone follows the naming rules you can register all of these component globally.



        For example in a BaseInputs.js



        const components = require.context(path, subfolder, regexForInputComponentFileNames)


        https://webpack.js.org/guides/dependency-management/#require-context



        components.keys().forEach(element => {
        const componentName = element.replace(/*Everything that is not the component name like .vue*/)

        Vue.component(componentName, () => import(path + componentName)
        })


        Then you just import 'path/to/BaseInputs' at the beginning of your application.



        As I said I have not tried it yet it might wrong somewhere or missing something I will give it a try as soon as I can.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 7 at 18:10

























        answered Nov 7 at 12:30









        eldo

        1,03421221




        1,03421221






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53188312%2fvuejs-async-and-dynamic-component%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            這個網誌中的熱門文章

            Xamarin.form Move up view when keyboard appear

            Post-Redirect-Get with Spring WebFlux and Thymeleaf

            Anylogic : not able to use stopDelay()