2017-10-16 5 views
2

: JS 공통 모듈에- VueJs 내 응용 프로그램에서 경로를 따라 한

const router = new Router({ 
    mode: 'history', 
    scrollBehavior:() => ({ y: 0 }), 
    routes: [ 
    { path: '/', component: HomePage, meta: { pageType: 'home'} }, 
    ], 
}); 

을하고 있습니다

const trackEvent = { 
    getFields:() => { 
    //here need to access meta fields(pageType) of current route. how is it possible ? 
    } 
} 
export default trackEvent; 

내가 메타에 액세스하려면 공통 모듈의 필드. 그게 어떻게 가능해 ?

답변

3

meta 속성은 Vue 인스턴스의 this.$route.meta을 통해 액세스 할 수 있습니다. 그냥 getFields 방법으로 전달하십시오.

export default { 
    created() { 
    let meta = getFields(this.$route.meta); 
    } 
} 

getFields: (meta) => { 
    console.log(meta); 

    return meta.fields.pageType; // not sure what you're trying to return exactly 
} 

현재 경로에 전달할 수없는 경우, 당신은 router 객체를 가져오고 그에서 현재 경로를 얻을해야합니다 :

import router from 'path/to/your/router' 

const trackEvent = { 
    getFields:() => { 
    let meta = router.currentRoute.meta; 
    console.log(meta); 

    return meta.fields.pageType; // not sure what you're trying to return exactly 
    } 
} 
export default trackEvent; 
+0

나는 구성 요소에서 메타 필드를 전달하지 않고 JS 모듈의 메타 필드에 직접 액세스해야하는 몇 가지 유스 케이스가있다. –

+0

자,이 경우에'router' 객체를 가져와야합니다. 내 편집을 참조하십시오. – thanksd