简体   繁体   English

将组件注册到现有的 Vue.js 实例

[英]Register a component to an existing Vue.js instance

I'm new to Vue.js.我是 Vue.js 的新手。

I want to register a local component as described over here:我想注册一个本地组件,如此处所述:

https://v2.vuejs.org/v2/guide/components.html#Local-Registration https://v2.vuejs.org/v2/guide/components.html#Local-Registration

The only issue is that I need to register a component to an existing Vue instance not while creating a new instance something like:唯一的问题是我需要将组件注册到现有的Vue 实例,而不是在创建新实例时,例如:

const app = new Vue({
    el: '#app'
});
    
app.component({
    'my-component': {
         template: '<div>A custom component!</div>'
    }
});

I have tried Vue.extend for that, but it is not working.我为此尝试了 Vue.extend,但它不起作用。

Edit:编辑:

Why I need this:为什么我需要这个:

I'm creating a 3rd party package which will have that component.我正在创建一个包含该组件的第 3 方包。 The framework on which this package is going to be installed will already have Vue.js included and a Vue instance.将要安装这个包的框架已经包含了 Vue.js 和一个 Vue 实例。 So if I include my package's JS before framework's JS, then it will give me Vue is undefined and if I include my package's JS after framework's JS, then it will give me component error as it has to be registered before the Vue instantiation.因此,如果我在框架的 JS 之前包含我的包的 JS,那么它会给我 Vue 是未定义的,如果我在框架的 JS 之后包含我的包的 JS,那么它会给我组件错误,因为它必须在 Vue 实例化之前注册。

Global components should be declared before new instance construct.全局组件应在新实例构造之前声明。

 Vue.component('my-component-a', { template: '<div>A custom component A!</div>' }); Vue.component('my-component-b', { template: '<div>A custom component B!</div>' }); const app1 = new Vue({ el: '#app1', template: '<div><my-component-a /><my-component-b /><my-component-c /></div>', components: { MyComponentC: { template: '<div>A custom component C!</div>' } } }); const app2 = new Vue({ el: '#app2', template: '<div><my-component-b /><my-component-a /><my-component-c /></div>' });
 <script src="https://cdn.jsdelivr.net/npm/vue@2.5.17/dist/vue.js"></script> <div id="app1"></div> <div id="app2"></div>

You don't have access to the C component from within your app2您无权从您的 app2 中访问 C 组件

You can't add components to an instance like that.您不能将组件添加到这样的实例中。 All you can do it adding them to the Vue constructor like usual:您可以像往常一样将它们添加到 Vue 构造函数中:

Vue.component('my-component', {
  template: '<div>A custom component!</div>'
});

const app = new Vue({
    el: '#app'
});

See a demo here: https://jsfiddle.net/Linusborg/kb9pbecq/在此处查看演示: https ://jsfiddle.net/Linusborg/kb9pbecq/

const app = new Vue({
  el: '#app',
  components: {
    'my-component': {template: '<div>Text in component</div>'}
   }
});

Basically, you can not register a component to a existing Vue instance while it was already rendered.But you can register a component before the Vue instance mounted into the dom element.基本上,您不能在已经渲染的现有 Vue 实例中注册组件。但是您可以在 Vue 实例安装到 dom 元素之前注册组件。

// local component
var child = {
  template: '<div>A custom component!</div>'
}

const app = new Vue({
   el: '#app',
   components: {
     Child: child
  }
})

or或者

// global component
Vue.component({
 'child': {
   template: '<div>A custom component!</div>'
 } })

Define the component first and then implement it.先定义组件,然后实现它。

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

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