Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Define global components #421

Closed
warrebuysse opened this issue Mar 23, 2017 · 10 comments
Closed

Define global components #421

warrebuysse opened this issue Mar 23, 2017 · 10 comments
Labels

Comments

@warrebuysse
Copy link

warrebuysse commented Mar 23, 2017

In every component or page we create we end up having tons of import "components/component/SomeComponent.vue" states for basic elements like regions, layouts, grids & columns. Is there a way in nuxt to define those components globally so they are known in subcomponents? Something like import "components/frame/Frame.vue" that contains the imports for regions, layouts, grids & columns.

This question is available on Nuxt.js community (#c363)
@alexchopin
Copy link
Member

alexchopin commented Mar 23, 2017

You can do it by creating a plugin file and register your components globally.

plugins/global.js

import Vue from 'vue'
import Grids from '../components/Grids.vue'

Vue.component('grids', Grids)

nuxt.config.js

plugins: ['~/plugins/global.js']

@warrebuysse
Copy link
Author

@alexchopin Then what should be in the Frame component? The import states for the other components?

Like this?

<script>

import BlMain from '~components/frame/main/Main.vue'
import BlRegion from '~components/frame/region/Region.vue'
import BlLayout from '~components/frame/layout/Layout.vue'
import BlGrid from '~components/frame/grid/Grid.vue'
import BlColumn from '~components/frame/column/Column.vue'

export default {
  components: {
    BlMain,
    BlRegion,
    BlLayout,
    BlGrid,
    BlColumn
  },
}
</script>

Do I need to import the Frame.vue component in the components as well?

import Frame from '~components/frame/Frame.vue'

PS: We are using adonuxt as a starter pack.

@warrebuysse
Copy link
Author

@alexchopin any idea?

@34r7h
Copy link

34r7h commented Aug 6, 2017

+1, seems like there should be a trivial mechanism to make utility components created in NUXT available globally.

For example, I'm including material design icons and making a utility component.

<template>
    <i class="material-icons">{{i}}</i>
</template>
<script>
  export default {
    props: ['i']
  }
</script>

Then following the advice of including in plugins on nuxt.config..

plugins: [
    { src: './components/util/Icon.vue' }
  ]

I would like this to register this icon component to all layouts, pages, and components.

@acidjazz
Copy link
Contributor

@irthos and @warrebuysse you ever figure this out?

@snsxn
Copy link

snsxn commented Aug 27, 2018

@acidjazz while importing the component in the plugin file look at the path and use it relative, not shortened "~"
Then you can use the component everywhere

@gkarmas
Copy link

gkarmas commented Sep 2, 2018

Confirming @acidjazz - just solved all of my issues because of @snsxn 's comment!

My working example:

global.js

import Vue from "vue";
import Badge from "~/components/Badge.vue";
import BaseAlert from "~/components/BaseAlert.vue";
import BaseButton from "~/components/BaseButton.vue";
import BaseCheckbox from "~/components/BaseCheckbox.vue";
import BaseInput from "~/components/BaseInput.vue";
import BasePagination from "~/components/BasePagination.vue";
import BaseProgress from "~/components/BaseProgress.vue";
import BaseRadio from "~/components/BaseRadio.vue";
import BaseSlider from "~/components/BaseSlider.vue";
import BaseSwitch from "~/components/BaseSwitch.vue";
import Card from "~/components/Card.vue";
import Icon from "~/components/Icon.vue";

Vue.component("badge", Badge);
Vue.component("baseAlert", BaseAlert);
Vue.component("baseButton", BaseButton);
Vue.component("baseCheckbox", BaseCheckbox);
Vue.component("baseInput", BaseInput);
Vue.component("basePagination", BasePagination);
Vue.component("baseProgress", BaseProgress);
Vue.component("baseRadio", BaseRadio);
Vue.component("baseSlider", BaseSlider);
Vue.component("baseSwitch", BaseSwitch);
Vue.component("card", Card);
Vue.component("icon", Icon);

nuxt.config.js

 plugins: [
    { src: "~/plugins/global.js" }
  ]

@acidjazz
Copy link
Contributor

acidjazz commented Sep 2, 2018

@gkarmas @snsxn @warrebuysse awesome!

i actually started working on a nuxt module to help register and use programmatic global components

here is an example repo setup using it as a plugin

https://github.com/acidjazz/nuxt-global-component

let me know your 2c on how i'm doing this if you don't mind!

@evry-johber
Copy link

evry-johber commented Sep 13, 2018

This is how I load all components which are placed directly under /components:

global-component-loader.js:

import Vue from 'vue'
import _ from 'lodash'

const components = require.context('@/components', false, /[A-Z]\w+\.(vue)$/) 
_.forEach(components.keys(), fileName => {
    const componentConfig = components(fileName)
    const componentName = fileName.split('/').pop().split('.')[0]
    Vue.component(componentName, componentConfig.default || componentConfig)
})

Nuxt config:

plugins: ['~/plugins/global-component-loader.js']

@lock
Copy link

lock bot commented Oct 31, 2018

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@lock lock bot locked as resolved and limited conversation to collaborators Oct 31, 2018
@danielroe danielroe added the 2.x label Jan 18, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

8 participants