<i id='Z4vp1'><tr id='Z4vp1'><dt id='Z4vp1'><q id='Z4vp1'><span id='Z4vp1'><b id='Z4vp1'><form id='Z4vp1'><ins id='Z4vp1'></ins><ul id='Z4vp1'></ul><sub id='Z4vp1'></sub></form><legend id='Z4vp1'></legend><bdo id='Z4vp1'><pre id='Z4vp1'><center id='Z4vp1'></center></pre></bdo></b><th id='Z4vp1'></th></span></q></dt></tr></i><div id='Z4vp1'><tfoot id='Z4vp1'></tfoot><dl id='Z4vp1'><fieldset id='Z4vp1'></fieldset></dl></div>

      <small id='Z4vp1'></small><noframes id='Z4vp1'>

      <legend id='Z4vp1'><style id='Z4vp1'><dir id='Z4vp1'><q id='Z4vp1'></q></dir></style></legend>
      • <bdo id='Z4vp1'></bdo><ul id='Z4vp1'></ul>

        <tfoot id='Z4vp1'></tfoot>

      1. 在 Vue.js 中动态实例化组件

        Dynamically instantiating a component in Vue.js(在 Vue.js 中动态实例化组件)
          <tbody id='htuOZ'></tbody>
        <legend id='htuOZ'><style id='htuOZ'><dir id='htuOZ'><q id='htuOZ'></q></dir></style></legend>

        <i id='htuOZ'><tr id='htuOZ'><dt id='htuOZ'><q id='htuOZ'><span id='htuOZ'><b id='htuOZ'><form id='htuOZ'><ins id='htuOZ'></ins><ul id='htuOZ'></ul><sub id='htuOZ'></sub></form><legend id='htuOZ'></legend><bdo id='htuOZ'><pre id='htuOZ'><center id='htuOZ'></center></pre></bdo></b><th id='htuOZ'></th></span></q></dt></tr></i><div id='htuOZ'><tfoot id='htuOZ'></tfoot><dl id='htuOZ'><fieldset id='htuOZ'></fieldset></dl></div>

      2. <small id='htuOZ'></small><noframes id='htuOZ'>

        <tfoot id='htuOZ'></tfoot>

                <bdo id='htuOZ'></bdo><ul id='htuOZ'></ul>
                • 本文介绍了在 Vue.js 中动态实例化组件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  按照本教程,我正在尝试以编程方式在我的页面上创建组件的实例.

                  Following this tutorial, I'm trying to programmatically create instances of a component on my page.

                  主要片段是这样的:

                  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)
                  

                  但是我得到两个错误:

                  1. 我尝试实例化的组件包含对存储的引用,这些引用不起作用:TypeError: Cannot read property 'state' of undefined".

                  对于片段的最后一行 (this.$refs.container.appendChild(instance.$el)) 我收到此错误:"Uncaught TypeError: Cannot read未定义的属性‘容器’"

                  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"

                  我真的不知道如何解决这个问题,如果任何 Vue.js 方面的强者可以给我一些提示,说明我为什么会遇到这些错误并解决它们,那将是非常棒的.

                  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.

                  推荐答案

                  1) 由于您手动实例化该组件并且它不属于您的主应用程序的组件树,因此不会自动将商店注入其中从你的根组件.当您实例化组件时,您必须手动将商店提供给构造函数..

                  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) 在 Vue 单文件组件 (SFC) 中,在默认导出 this 不引用 Vue 实例,因此您无权访问 $refs 或任何其他 Vue 实例属性/方法.要访问 Vue 实例,您需要将这一行 this.$refs.container.appendChild(instance.$el) 移动到默认导出中的某个位置,例如在 mounted 钩子或在你的 methods 之一内.

                  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.

                  请参阅此CodeSandbox,了解如何进行此操作的示例.

                  See this CodeSandbox for an example of how you may go about this.

                  这篇关于在 Vue.js 中动态实例化组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Javascript - fetch(Javascript - 获取)
                  how to get data from response from fetch javascript request(如何从 fetch javascript 请求的响应中获取数据)
                  javascript fetch automatically changes http with https(javascript fetch 自动将 http 更改为 https)
                  Why are these fetch methods asynchronous?(为什么这些 fetch 方法是异步的?)
                  JSON.parse() Vs. .json()(JSON.parse() 与..json())
                  How to use JSONP on fetch/axios cross-site requests(如何在 fetch/axios 跨站请求中使用 JSONP)

                    <tbody id='PZSXz'></tbody>

                        • <bdo id='PZSXz'></bdo><ul id='PZSXz'></ul>

                          <small id='PZSXz'></small><noframes id='PZSXz'>

                          <legend id='PZSXz'><style id='PZSXz'><dir id='PZSXz'><q id='PZSXz'></q></dir></style></legend>

                        • <i id='PZSXz'><tr id='PZSXz'><dt id='PZSXz'><q id='PZSXz'><span id='PZSXz'><b id='PZSXz'><form id='PZSXz'><ins id='PZSXz'></ins><ul id='PZSXz'></ul><sub id='PZSXz'></sub></form><legend id='PZSXz'></legend><bdo id='PZSXz'><pre id='PZSXz'><center id='PZSXz'></center></pre></bdo></b><th id='PZSXz'></th></span></q></dt></tr></i><div id='PZSXz'><tfoot id='PZSXz'></tfoot><dl id='PZSXz'><fieldset id='PZSXz'></fieldset></dl></div>
                        • <tfoot id='PZSXz'></tfoot>