No More Posting New Topics!

If you have a question or an issue, please start a thread in our Github Discussions Forum.
This forum is closed for new threads/ topics.

Navigation

    Quasar Framework

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search

    Global constants ?

    Framework
    4
    12
    1485
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • I
      Incremental last edited by

      Hello,
      I’ve set global constants in a Constantes.js :

      const Constantes = {
         // Serveur
         APP_VERSION: "0.1",
         SERVER_URL: "http://meteo.test",
         SERVER_MEDIA: "http://meteo.test/storage/app/media",
         //SERVER_URL: "http://192.168.0.3",
         //SERVER_MEDIA: "http://192.168.0.3/storage/app/media"
      
         // IHM
         button_color: "red"
      };
      Constantes.install = function (Vue) {
         Vue.prototype.$getConst = key => {
            return Constantes[key];
         };
      };
      
      export default Constantes;
      

      I use it successfully in my axios store :

      import Constantes from "../plugins/Constantes.js";
      
         this.$axios
            .post(Constantes.SERVER_URL + "/api/auth/login", user.data)
      

      Now I’d like to use such constants in my vue components
      For example to color a button.
      Is it possible to have something like :

              <q-btn
                to="/"
                :color="Constantes.theme_color"
                icon="fas fa-home"
              ></q-btn>
      

      but Constantes are not recognized…

      1 Reply Last reply Reply Quote 0
      • I
        Incremental last edited by Incremental

        Sorry should be read as :

                <q-btn
                  to="/"
                  :color="Constantes.button_color"
                  icon="fas fa-home"
                ></q-btn>
        
        T metalsadman 2 Replies Last reply Reply Quote 0
        • T
          turigeza @Incremental last edited by turigeza

          This post is deleted!
          1 Reply Last reply Reply Quote 0
          • metalsadman
            metalsadman @Incremental last edited by

            @Incremental you have a registered prototype, so use that in the template, $getConst(...).

            I 1 Reply Last reply Reply Quote 0
            • I
              Incremental @metalsadman last edited by

              @metalsadman
              Thanks for your answer, I tried

                      <q-btn
                        to="/"
                        push
                        :color="$getConst('Constantes.button_color')"
                        icon="fas fa-home"
                        glossy
                      ></q-btn>
              </template>
              
              <script>
              import Constantes from "../plugins/Constantes.js";
              

              but it doesn’t work…

              beets 1 Reply Last reply Reply Quote 0
              • beets
                beets @Incremental last edited by

                @Incremental I would convert your plugins/Constantes.js to a boot file in src/boot/Constantes.js

                const Constantes = {
                   // Serveur
                   APP_VERSION: "0.1",
                   SERVER_URL: "http://meteo.test",
                   SERVER_MEDIA: "http://meteo.test/storage/app/media",
                   //SERVER_URL: "http://192.168.0.3",
                   //SERVER_MEDIA: "http://192.168.0.3/storage/app/media"
                
                   // IHM
                   button_color: "red"
                };
                export default async ({ Vue }) => {
                  Vue.prototype.$Constantes = Constantes
                }
                export { Constantes }
                

                Add in quasar.conf.js:

                boot: [
                  'Constantes'
                ]
                
                

                Then in your axios store, you use it like this instead:

                import { Constantes } from "boot/Constantes.js";
                

                And finally in a template:

                        <q-btn
                          to="/"
                          :color="$Constantes.button_color"
                          icon="fas fa-home"
                        ></q-btn>
                
                1 Reply Last reply Reply Quote 1
                • T
                  turigeza last edited by turigeza

                  I was so wrong above I just deleted my answer : ) Sorry. And now @beets already posted a better one : )
                  https://codepen.io/turigeza/pen/MWjgwQp?editors=101

                  1 Reply Last reply Reply Quote 0
                  • I
                    Incremental last edited by Incremental

                    Thanks a lot all of you !!!
                    I love this framework and this kind community ;-))
                    @beets : all works fine.

                    Maybe this could be anuseful item in the Quasar doc site ?

                    metalsadman 1 Reply Last reply Reply Quote 0
                    • metalsadman
                      metalsadman @Incremental last edited by

                      @Incremental usage of boot file is well documented in docs.

                      1 Reply Last reply Reply Quote 0
                      • I
                        Incremental last edited by

                        In complement, is it possible to concatenate a string and a string constant in the template ?

                          <q-footer elevated>
                            <q-toolbar
                              :class="flex flex-center " + "$constantes.footer_bkg_color"
                            >
                        

                        Or should I have a :

                         computed: { }
                        

                        for setting the concatenation in a variable to use in the template ?
                        Thanks for your tricks 😉

                        metalsadman 1 Reply Last reply Reply Quote 0
                        • metalsadman
                          metalsadman @Incremental last edited by

                          @Incremental said in Global constants ?:

                          :class="flex flex-center " + “$constantes.footer_bkg_color”

                          like this :class="'flex flex-center ' + $constantes.footer_bkg_color" or yes better use computed prop.

                          1 Reply Last reply Reply Quote 0
                          • I
                            Incremental last edited by

                            Thanks a lot, I was not using the quotes correctly.
                            Have a good day !

                            1 Reply Last reply Reply Quote 0
                            • First post
                              Last post