Last active
April 19, 2018 12:20
-
-
Save mathieutu/6d700b4992c0013aa4939712bcf21b48 to your computer and use it in GitHub Desktop.
Component which selects <nuxt/vue-router> or <a href>, depending of the url you're passing to it, and projects dependencies.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<template> | |
<component :is="props.component" v-bind="props.attributes"> | |
<slot/> | |
</component> | |
</template> | |
<script> | |
import keysIn from 'lodash/keysIn'; | |
export default { | |
data() { | |
return { | |
availableComponents: keysIn(this.$options.components), | |
}; | |
}, | |
props: { | |
to: { | |
type: String, | |
required: true, | |
}, | |
}, | |
computed: { | |
isAbsolute() { | |
return this.to.match(/^(http(s)?|ftp):\/\//); | |
}, | |
props() { | |
if (!this.isAbsolute && this.availableComponents.includes('nuxt-link')) { | |
return { | |
component: 'nuxt-link', | |
attributes: { | |
to: this.to, | |
}, | |
}; | |
} | |
if (!this.isAbsolute && this.availableComponents.includes('router-link')) { | |
return { | |
component: 'router-link', | |
attributes: { | |
to: this.to, | |
}, | |
}; | |
} | |
return { | |
component: 'a', | |
attributes: { | |
href: this.to, | |
}, | |
}; | |
}, | |
}, | |
}; | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment