网站搜索

javascript - [Vue 警告]:无法解析组件


这是我的代码:

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

我哪里做错了?

答案#1

你必须在创建 Vue 视图模型之前,即在 new Vue({}) 之前,创建你的 Vue.component,或者在视图模型的 created 事件中创建你的 Vue.component。

这是第一个方法的 jsfiddle,这是显示第二个方法的 jsfiddle。