2017-03-10 96 views
2

最近我一直在前端和vue路由器中使用vue來構建SPA。Vue組件未在路由器視圖內呈現

我的問題是,我不能夠訪問主Vue的實例中定義的組件:每當我的#APP範圍的組件被渲染內做<element></element>

import ElementComponent from './Element'; 

const app = new Vue({ 
    el: '#app', 
    router, 
    components: { Element: ElementComponent } 
}); 

,但我不能用裏面的元素路線組件。

我的路線是這樣定義的:

var routes = [ 
{ path: '/section/part', component: require('../views/Part') }]; 

然後提供給Router實例:每當我試圖打電話<element></element>部件零件模板我在vuedevtools得到這裏面

new VueRouter({routes}); 

斷點:[Vue警告]:未知的自定義元素: - 您是否正確註冊組件?對於遞歸組件,確保提供「名稱」選項。 (位於C:\ Users \ Doe \ project \ js \ views \ Part.vue)

回答

2

您需要將組件導入views/Part組件。因此,請在主要Vue實例中做同樣的事情,但只能在零件組件中進行。

,如果你想使組件的全球我相信你需要做的確實使用

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

參考https://vuejs.org/v2/guide/components.html#Registration

+0

好吧,我給它一個嘗試在實例 – mentorgashi

+0

感謝您的平視,全球組件 – mentorgashi