[Vue warn]:未能解析组件

[Vue warn]: Failed to resolve component

本文关键字:组件 Vue warn      更新时间:2023-09-26

这是我的代码:

window.onload = function () {
var main = new Vue({
    el: '#main',
    data: {
        currentActivity: 'home'
    }
});
Vue.component('home', {
    template: '#home-template'
});
};

这是我的模板:

<head>
     <script type="text/x-template" id="home-template">
      <div class="banner dark">
        <div class="content">says some shit</div>
      </div>
      <div class="banner light">
        <div class="content">says some other shit</div>
      </div>
    </script>
</head>

当我加载页面时,我会收到以下警告:

[Vue warn]: Failed to resolve component: home

这个错误:

Uncaught TypeError: Cannot read property '_refID' of undefined

我做错了什么?

您必须在创建Vue ViewModel之前(即在new Vue({})之前)创建Vue.component,或者在created事件中的ViewModel中创建Vue.component

这是第一种方法的jsfiddle,这是第二种方法的jsonfiddle。