home.vue
组件中使用一个search-bar组件
,但我无法弄清楚为什么它不起作用。
文件夹结构
App.vue
components
home.vue
query.vue
common
HeaderBar.vue
SideBar.vue
SearchBar.vue
App.vue
script
import HeaderBar from './components/common/HeaderBar'
import SideBar from './components/common/SideBar'
export default {
name: 'App',
components: {
HeaderBar,
SideBar
},
html
<template>
<div id="app">
<HeaderBar></HeaderBar>
<SideBar></SideBar>
<main class="view">
<router-view></router-view>
</main>
</div>
</template>
以上内容完全正常。
Home.vue
脚本
import SearchBar from './common/SearchBar'
export default {
name: 'Home',
components: SearchBar
}
html
<template>
<section class="home">
<div class="hero-content">
<h1>Hello World</h1>
<SearchBar></SearchBar>
</div>
</section>
</template>
我收到了以下错误信息:
[Vue warn]: Invalid component name: "_compiled". Component names can only contain alphanumeric characters and the hyphen, and must start with a letter.
[Vue warn]: Invalid component name: "__file". Component names can only contain alphanumeric characters and the hyphen, and must start with a letter.
[Vue warn]: Unknown custom element: - did you register the component correctly? For recursive components, make sure to provide the "name" option.
found in
---> <Home> at src\components\Home.vue <App> at src\App.vue <Root>
main.js
文件,其中指定了你使用的所有组件的名称。似乎 Vue 无法识别你想要使用的组件。应该像这样:Vue.component('app-searchbar', SearchBar);
请参阅:https://vuejs.org/v2/guide/components-registration.html - Eugenioimport SearchBar from './components/common/SearchBar'
, 然后使用Vue.component('app-search', SearchBar);
让它在全局注册。所以现在可以在app.vue
中删除import
和components: {}
了吗? - user9389851