2

How do I implement something like this in Quasar without redefining the variable in each component:

<template>
    <div>Welcome to {{ APP_NAME }}.</div>
</template>

My app was setup using Quasar CLI which asked for an app name during setup, so I imagine that is stored somewhere as a global variable or something I can access.

Failing that, maybe Vue 3 has a way of doing this.

Nikola Pavicevic
  • 21,952
  • 9
  • 25
  • 46
TinyTiger
  • 1,801
  • 7
  • 47
  • 92

5 Answers5

6

Quasar doesn't use a main.js file explicitly. Most of the suggested answers won't work when creating a project using quasar cli. Using quasar.config.js might work but it's still not exactly the right place to do it.

Since you used the quasar cli you need to add a boot file with quasar new boot.

This will generate the file ezglobals.js in your src/boot folder: quasar new boot ezglobals

Your code in your ezglobals.js file will look something like this after editing:

import { boot } from 'quasar/wrappers'
export default boot(({ app }) => {
  app.config.globalProperties.$APP_NAME = 'The name of your app';
})

Finally in quasar.config.js add ezglobals.js to the boot array:

...
boot: [
      'ezglobals.js'
    ]
...
Jerm86
  • 111
  • 1
  • 5
2

Yo can create global variable in Vue 3:

const globalVariable = 'app name'
app.config.globalProperties.$appName = globalVariable

and then show it in any template like:

<template>
 <div>Welcome to {{ $appName }}.</div>
</template>
Nikola Pavicevic
  • 21,952
  • 9
  • 25
  • 46
  • 1
    I might be daft, but I don't know where to put that first code. It seems Quasar dosen't come with a main.js file if that is where you expected that to do. – TinyTiger Jan 27 '22 at 23:01
  • @TinyTiger hey mate , not sure about Quasar, try like in [this post](https://stackoverflow.com/questions/50823340/with-vue-cli-where-do-i-declare-my-global-variables), just convert it to Vue3 – Nikola Pavicevic Jan 27 '22 at 23:25
  • [the documentation](https://quasar.dev/quasar-cli-webpack/boot-files#axios) recommends a boot file. – Stefan Krüger s-light May 24 '22 at 06:58
2

There are a few ways how you could do it.

The name you specified during project creation using Quasar CLI is stored in your package.json file ("name": "…"). You can access package.json vars like that:

process.env.npm_package_name

Here is a link with more info about it: https://docs.npmjs.com/cli/v6/using-npm/scripts#packagejson-vars

To make this globally available you can create a boot file specifying a global variable.

Here you can read more on how to create and use boot files (boot is a folder in your project created by quasar cli): https://quasar.dev/quasar-cli/boot-files

Here you can find more info to define global variables: https://v3.vuejs.org/api/application-config.html#globalproperties

David Wolf
  • 1,400
  • 1
  • 9
  • 18
sma
  • 76
  • 1
  • 6
0

You should import Quasar in main.js

import { useQuasar } from 'Quasar'


createApp(App).use(Quasar, { config: {} }).mount('#app')
RaceWalker
  • 12
  • 2
0

not exactly global - you have to make it available in every component you want to use it.

define some env options in your quasar.config.js file:

const packageInfo = require('./package.json')

const { configure } = require('quasar/wrappers');

module.exports = configure(function (ctx) {
    return {
        // ....
        build: {
            // ....
            env: {
                // https://forum.quasar-framework.org/topic/6853/auto-generate-a-build-number-in-spa/15?_=1653270667151
                // https://quasar.dev/quasar-cli-webpack/handling-process-env#caveats
                // TEST: "42",
                appinfo: {
                    name: packageInfo.name,
                    version: packageInfo.version,
                    productName: packageInfo.productName,
                    description: packageInfo.description,
                    projectUrl: packageInfo.projectUrl,
                    previewUrl: packageInfo.previewUrl,
                },
            },
        },
        // ....
    }
});

than you need to include something like this in YourComponent.vue file:

<template>
    <q-page
        class="flex column"
        style="align-items: center;"
    >
        <section>
            <h4>{{ appinfo.productName }}</h4>
            <p>
                version: v{{ appinfo.version }}
            </p>
            <p>
                {{ appinfo.description }}<br>
                find the project repository at <br>
                <a
                    target="_blank"
                    :href="appinfo.projectUrl"
                >
                    {{ appinfo.projectUrl }}
                </a>
            </p>
            <p>
                a live preview version is hosted at<br>
                <a
                    target="_blank"
                    :href="appinfo.previewUrl"
                >
                    {{ appinfo.previewUrl }}
                </a>
            </p>
        </section>
    </q-page>
</template>

<script setup>
// https://quasar.dev/quasar-cli-webpack/handling-process-env#caveats
// console.log(process.env.TEST)
const appinfo = process.env.appinfo
</script>

or for the script part the older way:

<script>
export default {
    name: 'AboutPage',
    data () {
        // https://quasar.dev/quasar-cli-webpack/handling-process-env#caveats
        // console.log(process.env.TEST)
        return {
            appinfo: process.env.appinfo,
        }
    }
}
</script>