Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I register component globally in Quasar?

I have a component which I'll be using in every page of my web-app. To simplify my work how do I register this component globally? I used nuxt once and it was so easy by npm @nuxtjs/global-components. What is process with Quasar? Thank you!

like image 567
Eldar Tailov Avatar asked Sep 15 '20 04:09

Eldar Tailov


People also ask

How do I register a component on global Vue?

When we want to globally register a component in Vue, we need to do it in this file. All you have to do is import your component, as you usually would, and then register it using app. component . import { createApp } from 'vue' import App from './App.

How do I register a component on vue3?

import Vue from "vue";import PopupWindow from "./components/PopupWindow";import App from "./App. vue";Vue. component("PopupWindow", PopupWindow); // global registration - can be used anywherenew Vue({ render: (h) => h(App),}). $mount("#app");

Is Quasar compatible with Vue 3?

This means that your app code (Vue components, directives, etc) should be Vue 3 compliant too, not just the Quasar UI source-code. If you are using additional libraries in your app, please make sure that you are using their Vue 3 versions. Quasar UI v2 is not just a port to Vue 3 and Composition API.


Video Answer


3 Answers

You can create a boot file with all your global components, e.g. global-components.js

There you need to import your components and apply them to Vue:

import Vue from 'vue'
import MyComponent from '../component/MyComponent.vue'

Vue.component('my-component', MyComponent)

Lastly you need to call your boot file in quasar.conf.js

boot: ["global-components"]

More info

like image 61
David Go Avatar answered Oct 23 '22 07:10

David Go


In Quasar 2:

import { boot } from 'quasar/wrappers'
import MyComponent from '../components/MyComponent.vue'

export default boot(async ({ app }) => {
  app.component('my-component', MyComponent)
})
like image 25
fromani Avatar answered Oct 23 '22 09:10

fromani


I use this solution and it helps us to dynamically register component

Create a file in src/boot folder for example i created register-my-component.js and white this code:

// src/boot/register-my-component.js
import upperFirst from "lodash/upperFirst";
import camelCase from "lodash/camelCase";

export default ({ app }) => {
  const requireComponent = require.context(
    "src/components",
    true,
    /[A-Z-_]\w+\.(vue)$/
  );

  requireComponent.keys().forEach((fileName) => {
    const componentConfig = requireComponent(fileName);
    const componentName = upperFirst(
      camelCase(
        fileName
          .split("/")
          .pop()
          .replace(/\.\w+$/, "")
      )
    );
    app.component(componentName, componentConfig.default || componentConfig);
  });
};

Now register this boot file in quasar.conf.js. you can find it in the root directory

// app boot file (/src/boot)
// --> boot files are part of "main.js"
// https://quasar.dev/quasar-cli/boot-files
boot: [ ..., "register-my-component" ],

Now you don't need to import and register components anymore (NOTE: as you can see only components in src/component register automatically. So if you write your component in other paths you need to import and register that)

like image 32
Kasra Karami Avatar answered Oct 23 '22 08:10

Kasra Karami