Vue.js:检查组件是否存在

viv*_*ino 15 javascript vue.js

ready:只有当某些组件不存在时(我们没有在HTML中声明),我才需要在根实例中做一些事情.

如何检查组件是否存在?

Has*_*ami 14

我们可以通过获取所加载(全球和/或本地)的部件从化选项一个Vue公司的实例是可用的列表vm.$options,其中vm是当前Vue的实例.

vm.$optionsproperty保存Vue实例的所有选项.例如,vm.$options.components返回包含当前Vue instace的已加载组件的对象vm.

但是,根据组件的注册方式,无论是全局通过Vue.component()还是在Vue实例选项中本地注册,结构vm.$options.components都会有所不同.

如果组件是全局注册的,则组件将添加到vm.$options.components[[Prototype]]链接或其中__proto__.

如果组件在Vue实例选项中本地注册,则组件将vm.$options.components直接作为其自己的属性添加到对象中.这样我们就不必走原型链来找到组件.


在以下示例中,我们将了解如何在两种情况下访问已加载的组件.

请注意代码中与本地注册组件相关的注释// [1]// [2]注释.

// the globally registered component
Vue.component('global-child', {
  template: '<h2>A message from the global component</h2>'
});

var localComponent = Vue.extend({ template: '<h2>A message from the local component</h2>' });


// the root view model
new Vue({
  el: 'body',
  data: {
    allComponents: [],
    localComponents: [],
    globalComponentIsLoaded: false
  },
  // local registered components
  components: { // [1]
    'local-child': localComponent
  },
  ready: function() {    
    this.localComponents = Object.keys(this.$options.components); // [2]
    
    this.allComponents = loadedComponents.call(this);
    this.globalComponentIsLoaded = componentExists.call(this, 'global-child');
  }
});

function loadedComponents() {
  var loaded = [];
  var components = this.$options.components;
  for (var key in components) {
    loaded.push(key);
  }
  return loaded;
}

function componentExists(component) {
  var components = loadedComponents.call(this);
  if (components.indexOf(component) !== -1) {
    return true;
  }
  return false;
}
Run Code Online (Sandbox Code Playgroud)
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/1.0.24/vue.js"></script>
<h1>A message from the root Vue instance</h1>
<p>All loaded components are: {{ allComponents | json }}</p>
<p>local components are: {{ localComponents | json }}</p>
<p><code>&lt;global-child&gt;</code> component is loaded: <strong>{{ globalComponentIsLoaded }}</strong></p>
<global-child></global-child>
<local-child></local-child>
Run Code Online (Sandbox Code Playgroud)


Gly*_*ine 8

我不确定您是否需要动态方法,但这可能有助于确定组件是否存在:

Vue.options.components['CompOne']
Run Code Online (Sandbox Code Playgroud)

发现:

https://github.com/vuejs/Discussion/issues/140


Shi*_*127 6

检查全局组件是否存在:

let componentExists = 'componentName' in Vue.options.components
Run Code Online (Sandbox Code Playgroud)

要检查组件中是否存在导入的组件:

let componentExists = 'componentName' in this.$options.components
Run Code Online (Sandbox Code Playgroud)


Al-*_*min 5

获取当前组件导入的组件

this.$options.components[findComponentName]
Run Code Online (Sandbox Code Playgroud)

获取全局组件

Vue.$options.components[findComponentName]
Run Code Online (Sandbox Code Playgroud)


viv*_*ino 2

我真的希望有一个比这更好的答案,但目前这解决了问题。

this在准备就绪时,我通过(也可以是)访问子元素Vue并检查它们的名称是否是我所期望的:

    ready: function() {

        for (var i = 0; i < this.$children.length; i++) {
            if (
                   this.$children[i].$options.name == 'my_component_a'
                || this.$children[i].$options.name == 'my_component_b'
                || this.$children[i].$options.name == 'my_component_c'
            ) {
                //do stuff
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

如果您之前在模板中为它们分配了引用,您也可以直接访问它们: //template:

<comp v-ref:my_component_ref></comp>
Run Code Online (Sandbox Code Playgroud)

然后从Vue组件准备好:

if (this.$refs.my_component_ref){
//do stuff
}
Run Code Online (Sandbox Code Playgroud)