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.
vuejs2
add a comment |
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.
vuejs2
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
add a comment |
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.
vuejs2
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
vuejs2
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
add a comment |
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
add a comment |
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.
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
edited Nov 7 at 18:10
answered Nov 7 at 12:30
eldo
1,03421221
1,03421221
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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