Dynamically instantiating a component in Vue.js
Following this tutorial, I'm trying to programmatically create instances of a component on my page.
The main snippet is this:
import Button from 'Button.vue'
import Vue from 'vue'
var ComponentClass = Vue.extend(Button)
var instance = new ComponentClass()
instance.$mount()
this.$refs.container.appendChild(instance.$el)
However I get two errors:
The component I'm trying to instantiate contains references to the store, and these don't work: "TypeError: Cannot read property 'state' of undefined".
For the last line of the snippet (
this.$refs.container.appendChild(instance.$el)
) I get this error: "Uncaught TypeError: Cannot read property 'container' of undefined"
I'm really not sure how to troubleshoot this, if anyone strong in Vue.js could give me some hint as to why I'm getting these errors and to solve them that would be terrific.
vue.js components
|
show 3 more comments
Following this tutorial, I'm trying to programmatically create instances of a component on my page.
The main snippet is this:
import Button from 'Button.vue'
import Vue from 'vue'
var ComponentClass = Vue.extend(Button)
var instance = new ComponentClass()
instance.$mount()
this.$refs.container.appendChild(instance.$el)
However I get two errors:
The component I'm trying to instantiate contains references to the store, and these don't work: "TypeError: Cannot read property 'state' of undefined".
For the last line of the snippet (
this.$refs.container.appendChild(instance.$el)
) I get this error: "Uncaught TypeError: Cannot read property 'container' of undefined"
I'm really not sure how to troubleshoot this, if anyone strong in Vue.js could give me some hint as to why I'm getting these errors and to solve them that would be terrific.
vue.js components
do you have an element with referencecontainer
?<div ref="container"></div>
– cal_br_mar
Nov 9 at 21:28
Good catch, unfortunately after adding<div ref="container"></div>
inside my template, I still get the same two errors.
– drake035
Nov 9 at 21:54
can you publish a running example?
– cal_br_mar
Nov 9 at 22:02
Well my whole code is exposed. Plus I don't know how to upload a full Webpack/Vue project on sites like Jsfiddle, is that even possible?
– drake035
Nov 9 at 22:21
I think dom is not created yet when you call$refs
. instead, put it inside a method in the export module like the tutorial you cited. codesandbox
– Socrates Tuas
Nov 10 at 7:23
|
show 3 more comments
Following this tutorial, I'm trying to programmatically create instances of a component on my page.
The main snippet is this:
import Button from 'Button.vue'
import Vue from 'vue'
var ComponentClass = Vue.extend(Button)
var instance = new ComponentClass()
instance.$mount()
this.$refs.container.appendChild(instance.$el)
However I get two errors:
The component I'm trying to instantiate contains references to the store, and these don't work: "TypeError: Cannot read property 'state' of undefined".
For the last line of the snippet (
this.$refs.container.appendChild(instance.$el)
) I get this error: "Uncaught TypeError: Cannot read property 'container' of undefined"
I'm really not sure how to troubleshoot this, if anyone strong in Vue.js could give me some hint as to why I'm getting these errors and to solve them that would be terrific.
vue.js components
Following this tutorial, I'm trying to programmatically create instances of a component on my page.
The main snippet is this:
import Button from 'Button.vue'
import Vue from 'vue'
var ComponentClass = Vue.extend(Button)
var instance = new ComponentClass()
instance.$mount()
this.$refs.container.appendChild(instance.$el)
However I get two errors:
The component I'm trying to instantiate contains references to the store, and these don't work: "TypeError: Cannot read property 'state' of undefined".
For the last line of the snippet (
this.$refs.container.appendChild(instance.$el)
) I get this error: "Uncaught TypeError: Cannot read property 'container' of undefined"
I'm really not sure how to troubleshoot this, if anyone strong in Vue.js could give me some hint as to why I'm getting these errors and to solve them that would be terrific.
vue.js components
vue.js components
edited Nov 14 at 18:48
asked Nov 9 at 21:12
drake035
492123994
492123994
do you have an element with referencecontainer
?<div ref="container"></div>
– cal_br_mar
Nov 9 at 21:28
Good catch, unfortunately after adding<div ref="container"></div>
inside my template, I still get the same two errors.
– drake035
Nov 9 at 21:54
can you publish a running example?
– cal_br_mar
Nov 9 at 22:02
Well my whole code is exposed. Plus I don't know how to upload a full Webpack/Vue project on sites like Jsfiddle, is that even possible?
– drake035
Nov 9 at 22:21
I think dom is not created yet when you call$refs
. instead, put it inside a method in the export module like the tutorial you cited. codesandbox
– Socrates Tuas
Nov 10 at 7:23
|
show 3 more comments
do you have an element with referencecontainer
?<div ref="container"></div>
– cal_br_mar
Nov 9 at 21:28
Good catch, unfortunately after adding<div ref="container"></div>
inside my template, I still get the same two errors.
– drake035
Nov 9 at 21:54
can you publish a running example?
– cal_br_mar
Nov 9 at 22:02
Well my whole code is exposed. Plus I don't know how to upload a full Webpack/Vue project on sites like Jsfiddle, is that even possible?
– drake035
Nov 9 at 22:21
I think dom is not created yet when you call$refs
. instead, put it inside a method in the export module like the tutorial you cited. codesandbox
– Socrates Tuas
Nov 10 at 7:23
do you have an element with reference
container
? <div ref="container"></div>
– cal_br_mar
Nov 9 at 21:28
do you have an element with reference
container
? <div ref="container"></div>
– cal_br_mar
Nov 9 at 21:28
Good catch, unfortunately after adding
<div ref="container"></div>
inside my template, I still get the same two errors.– drake035
Nov 9 at 21:54
Good catch, unfortunately after adding
<div ref="container"></div>
inside my template, I still get the same two errors.– drake035
Nov 9 at 21:54
can you publish a running example?
– cal_br_mar
Nov 9 at 22:02
can you publish a running example?
– cal_br_mar
Nov 9 at 22:02
Well my whole code is exposed. Plus I don't know how to upload a full Webpack/Vue project on sites like Jsfiddle, is that even possible?
– drake035
Nov 9 at 22:21
Well my whole code is exposed. Plus I don't know how to upload a full Webpack/Vue project on sites like Jsfiddle, is that even possible?
– drake035
Nov 9 at 22:21
I think dom is not created yet when you call
$refs
. instead, put it inside a method in the export module like the tutorial you cited. codesandbox– Socrates Tuas
Nov 10 at 7:23
I think dom is not created yet when you call
$refs
. instead, put it inside a method in the export module like the tutorial you cited. codesandbox– Socrates Tuas
Nov 10 at 7:23
|
show 3 more comments
1 Answer
1
active
oldest
votes
1) Since you're manually instantiating that component and it doesn't belong to your main app's component tree, the store won't be automatically injected into it from your root component. You'll have to manually provide the store to the constructor when you instantiate the component ..
import ProjectRow from "./ProjectRow.vue";
import Vue from "vue";
import store from "../store";
let ProjectRowClass = Vue.extend(ProjectRow);
let ProjectRowInstance = new ProjectRowClass( store );
2) In a Vue Single File Component (SFC), outside of the default export this
doesn't refer to the Vue instance, so you don't have access to $refs
or any other Vue instance property/method. To gain access to the Vue instance you'll need to move this line this.$refs.container.appendChild(instance.$el)
somewhere inside the default export, for example in the mounted
hook or inside one of your methods
.
See this CodeSandbox for an example of how you may go about this.
This seems to work! Any idea how I could inject the component to a different component than the one we're in? So instead ofthis.$refs.container.appendChild(ProjectRowInstance.$el);
, something likeanotherComponent.$refs.container.appendChild(ProjectRowInstance.$el);
. Is there a way to achieve that?
– drake035
Nov 14 at 19:20
@drake035 You should be able to traverse your component tree up by using$root
and$parent
, and down by using$refs
as explained in the docs. See this jsfiddle for example.
– Husam Ibrahim
Nov 15 at 3:53
Thanks a lot for your help!
– drake035
Nov 16 at 19:38
add a comment |
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',
autoActivateHeartbeat: false,
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
);
);
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%2f53233331%2fdynamically-instantiating-a-component-in-vue-js%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
1) Since you're manually instantiating that component and it doesn't belong to your main app's component tree, the store won't be automatically injected into it from your root component. You'll have to manually provide the store to the constructor when you instantiate the component ..
import ProjectRow from "./ProjectRow.vue";
import Vue from "vue";
import store from "../store";
let ProjectRowClass = Vue.extend(ProjectRow);
let ProjectRowInstance = new ProjectRowClass( store );
2) In a Vue Single File Component (SFC), outside of the default export this
doesn't refer to the Vue instance, so you don't have access to $refs
or any other Vue instance property/method. To gain access to the Vue instance you'll need to move this line this.$refs.container.appendChild(instance.$el)
somewhere inside the default export, for example in the mounted
hook or inside one of your methods
.
See this CodeSandbox for an example of how you may go about this.
This seems to work! Any idea how I could inject the component to a different component than the one we're in? So instead ofthis.$refs.container.appendChild(ProjectRowInstance.$el);
, something likeanotherComponent.$refs.container.appendChild(ProjectRowInstance.$el);
. Is there a way to achieve that?
– drake035
Nov 14 at 19:20
@drake035 You should be able to traverse your component tree up by using$root
and$parent
, and down by using$refs
as explained in the docs. See this jsfiddle for example.
– Husam Ibrahim
Nov 15 at 3:53
Thanks a lot for your help!
– drake035
Nov 16 at 19:38
add a comment |
1) Since you're manually instantiating that component and it doesn't belong to your main app's component tree, the store won't be automatically injected into it from your root component. You'll have to manually provide the store to the constructor when you instantiate the component ..
import ProjectRow from "./ProjectRow.vue";
import Vue from "vue";
import store from "../store";
let ProjectRowClass = Vue.extend(ProjectRow);
let ProjectRowInstance = new ProjectRowClass( store );
2) In a Vue Single File Component (SFC), outside of the default export this
doesn't refer to the Vue instance, so you don't have access to $refs
or any other Vue instance property/method. To gain access to the Vue instance you'll need to move this line this.$refs.container.appendChild(instance.$el)
somewhere inside the default export, for example in the mounted
hook or inside one of your methods
.
See this CodeSandbox for an example of how you may go about this.
This seems to work! Any idea how I could inject the component to a different component than the one we're in? So instead ofthis.$refs.container.appendChild(ProjectRowInstance.$el);
, something likeanotherComponent.$refs.container.appendChild(ProjectRowInstance.$el);
. Is there a way to achieve that?
– drake035
Nov 14 at 19:20
@drake035 You should be able to traverse your component tree up by using$root
and$parent
, and down by using$refs
as explained in the docs. See this jsfiddle for example.
– Husam Ibrahim
Nov 15 at 3:53
Thanks a lot for your help!
– drake035
Nov 16 at 19:38
add a comment |
1) Since you're manually instantiating that component and it doesn't belong to your main app's component tree, the store won't be automatically injected into it from your root component. You'll have to manually provide the store to the constructor when you instantiate the component ..
import ProjectRow from "./ProjectRow.vue";
import Vue from "vue";
import store from "../store";
let ProjectRowClass = Vue.extend(ProjectRow);
let ProjectRowInstance = new ProjectRowClass( store );
2) In a Vue Single File Component (SFC), outside of the default export this
doesn't refer to the Vue instance, so you don't have access to $refs
or any other Vue instance property/method. To gain access to the Vue instance you'll need to move this line this.$refs.container.appendChild(instance.$el)
somewhere inside the default export, for example in the mounted
hook or inside one of your methods
.
See this CodeSandbox for an example of how you may go about this.
1) Since you're manually instantiating that component and it doesn't belong to your main app's component tree, the store won't be automatically injected into it from your root component. You'll have to manually provide the store to the constructor when you instantiate the component ..
import ProjectRow from "./ProjectRow.vue";
import Vue from "vue";
import store from "../store";
let ProjectRowClass = Vue.extend(ProjectRow);
let ProjectRowInstance = new ProjectRowClass( store );
2) In a Vue Single File Component (SFC), outside of the default export this
doesn't refer to the Vue instance, so you don't have access to $refs
or any other Vue instance property/method. To gain access to the Vue instance you'll need to move this line this.$refs.container.appendChild(instance.$el)
somewhere inside the default export, for example in the mounted
hook or inside one of your methods
.
See this CodeSandbox for an example of how you may go about this.
answered Nov 12 at 6:20
Husam Ibrahim
2,548315
2,548315
This seems to work! Any idea how I could inject the component to a different component than the one we're in? So instead ofthis.$refs.container.appendChild(ProjectRowInstance.$el);
, something likeanotherComponent.$refs.container.appendChild(ProjectRowInstance.$el);
. Is there a way to achieve that?
– drake035
Nov 14 at 19:20
@drake035 You should be able to traverse your component tree up by using$root
and$parent
, and down by using$refs
as explained in the docs. See this jsfiddle for example.
– Husam Ibrahim
Nov 15 at 3:53
Thanks a lot for your help!
– drake035
Nov 16 at 19:38
add a comment |
This seems to work! Any idea how I could inject the component to a different component than the one we're in? So instead ofthis.$refs.container.appendChild(ProjectRowInstance.$el);
, something likeanotherComponent.$refs.container.appendChild(ProjectRowInstance.$el);
. Is there a way to achieve that?
– drake035
Nov 14 at 19:20
@drake035 You should be able to traverse your component tree up by using$root
and$parent
, and down by using$refs
as explained in the docs. See this jsfiddle for example.
– Husam Ibrahim
Nov 15 at 3:53
Thanks a lot for your help!
– drake035
Nov 16 at 19:38
This seems to work! Any idea how I could inject the component to a different component than the one we're in? So instead of
this.$refs.container.appendChild(ProjectRowInstance.$el);
, something like anotherComponent.$refs.container.appendChild(ProjectRowInstance.$el);
. Is there a way to achieve that?– drake035
Nov 14 at 19:20
This seems to work! Any idea how I could inject the component to a different component than the one we're in? So instead of
this.$refs.container.appendChild(ProjectRowInstance.$el);
, something like anotherComponent.$refs.container.appendChild(ProjectRowInstance.$el);
. Is there a way to achieve that?– drake035
Nov 14 at 19:20
@drake035 You should be able to traverse your component tree up by using
$root
and $parent
, and down by using $refs
as explained in the docs. See this jsfiddle for example.– Husam Ibrahim
Nov 15 at 3:53
@drake035 You should be able to traverse your component tree up by using
$root
and $parent
, and down by using $refs
as explained in the docs. See this jsfiddle for example.– Husam Ibrahim
Nov 15 at 3:53
Thanks a lot for your help!
– drake035
Nov 16 at 19:38
Thanks a lot for your help!
– drake035
Nov 16 at 19:38
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
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%2f53233331%2fdynamically-instantiating-a-component-in-vue-js%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
do you have an element with reference
container
?<div ref="container"></div>
– cal_br_mar
Nov 9 at 21:28
Good catch, unfortunately after adding
<div ref="container"></div>
inside my template, I still get the same two errors.– drake035
Nov 9 at 21:54
can you publish a running example?
– cal_br_mar
Nov 9 at 22:02
Well my whole code is exposed. Plus I don't know how to upload a full Webpack/Vue project on sites like Jsfiddle, is that even possible?
– drake035
Nov 9 at 22:21
I think dom is not created yet when you call
$refs
. instead, put it inside a method in the export module like the tutorial you cited. codesandbox– Socrates Tuas
Nov 10 at 7:23