简体   繁体   English

在 Vue.js 中使用 JS 将组件挂载到 DOM 中

[英]Mount component into DOM with JS in Vue.js

I'm making a GUI for a webgame with Vue.JS, now I'm using http-vue-loader to avoid using Webpack and similar (sorry but I really hate it).我正在使用 Vue.JS 为网页游戏制作 GUI,现在我使用http-vue-loader来避免使用 Webpack 和类似的东西(对不起,但我真的很讨厌它)。

Now, usually what I do for get my component into my DOM is this:现在,通常我将我的组件放入我的 DOM 是这样的:

GUI.js GUI.js

    // Creating Vue.js istance for GUI
    Pixua.GUI = new Vue({
      el: '#GUI',
      components: {
        'Debug': httpVueLoader('./gui/windows/Debug.vue')
      },
      data: {
        row: 0,
        column: 0
      }
    });

Index.html索引.html

<div id="GUI">
      <windows id="windows">
           <debug v-bind:row="row" v-bind:column="column" ></debug>
      </windows>
</div>

Debug.vue (it's my component) Debug.vue (这是我的组件)

<template>
  <px-window title="Debug" :width="200" :is-open.sync="isOpen">
      Casella cliccata:
      <ul>
        <li>Riga: {{row}}</li>
        <li>Colonna: {{column}}</li>
      </ul>
  </px-window>
</template>

<script>
module.exports = {
    props: ['row','column'],
    data: function() {
        return {
            isOpen: true
        }
    }
}

</script>

<style>
</style>

Now my question is: is possible to get the component mounted/rendered into the DOM dinamically with JS (via GUI.js) and without specifying it into the index.html (so without <debug v-bind:row="row" v-bind:column="column" ></debug> )?现在我的问题是:是否可以使用 JS(通过 GUI.js)动态地将组件安装/渲染到 DOM 中,而无需将其指定到 index.html 中(因此没有<debug v-bind:row="row" v-bind:column="column" ></debug> )?

You can use Dynamic components您可以使用动态组件

<component v-bind:is="myComponent"></component>

And then change the value of myComponent to the component you want to load.然后将 myComponent 的值更改为您要加载的组件。

data() {
    return {
        myComponent: 'debug'
    }
}

Will render the debug component将渲染调试组件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM