Dynamically instantiating a component in Vue.js










0














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:



  1. The component I'm trying to instantiate contains references to the store, and these don't work: "TypeError: Cannot read property 'state' of undefined".


  2. 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.










share|improve this question























  • 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
















0














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:



  1. The component I'm trying to instantiate contains references to the store, and these don't work: "TypeError: Cannot read property 'state' of undefined".


  2. 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.










share|improve this question























  • 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














0












0








0







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:



  1. The component I'm trying to instantiate contains references to the store, and these don't work: "TypeError: Cannot read property 'state' of undefined".


  2. 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.










share|improve this question















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:



  1. The component I'm trying to instantiate contains references to the store, and these don't work: "TypeError: Cannot read property 'state' of undefined".


  2. 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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 14 at 18:48

























asked Nov 9 at 21:12









drake035

492123994




492123994











  • 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

















  • 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
















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













1 Answer
1






active

oldest

votes


















2





+50









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.






share|improve this answer




















  • 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










  • Thanks a lot for your help!
    – drake035
    Nov 16 at 19:38










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
);



);













draft saved

draft discarded


















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









2





+50









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.






share|improve this answer




















  • 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










  • Thanks a lot for your help!
    – drake035
    Nov 16 at 19:38















2





+50









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.






share|improve this answer




















  • 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










  • Thanks a lot for your help!
    – drake035
    Nov 16 at 19:38













2





+50







2





+50



2




+50




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.






share|improve this answer












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.







share|improve this answer












share|improve this answer



share|improve this answer










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 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










  • 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










  • @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

















draft saved

draft discarded
















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)