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

    [Solved] How to dynamically change layout in router base on platform

    Framework
    layout route.js
    3
    8
    1558
    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.
    • F
      folami last edited by folami

      How do I achieve this…

      const router = [
        {
           path: '/someroute'
           component:  () => Platform.is.mobile ? import('layouts/mainLayout.vue') : import('layouts/mobileView.vue')
         ...
        }
      ]
      

      Thanks

      1 Reply Last reply Reply Quote 0
      • Pratik Patel
        Pratik Patel last edited by

        Try this code.

        import { Platform } from 'quasar'
        
        let custom_page;
        if(Platform.is.mobile){
          custom_page ="mobileView.vue"
        }
        else{
            custom_page ="mainLayout.vue"
        }
        

        In route DIct .

        { path: '/', name: "/", component: () => import('layouts/' + custom_page)},
        
        F 1 Reply Last reply Reply Quote 0
        • F
          folami @Pratik Patel last edited by folami

          @Pratik-Patel, I have thought about this but my app is in SSR mode and Platform Plugin needs to parse ssrContext during ssr rendering which is only available in boot file plugins and prefetch but not in router

          function (ssrContext) {
            const platform = process.env.SERVER
              ? Platform.parseSSR(ssrContext)
              : Platform // otherwise we're on client
          
            // platform is equivalent to the global import as in non-SSR builds
          }
          
          T 1 Reply Last reply Reply Quote 0
          • T
            tof06 @folami last edited by

            @folami As far as I know, ssrContext is available in router :

            //src/router/index.js
            import Vue from 'vue'
            import VueRouter from 'vue-router'
            
            import routes from './routes'
            
            Vue.use(VueRouter)
            
            /*
             * If not building with SSR mode, you can
             * directly export the Router instantiation
             */
            
            export default function({ store, ssrContext }) {
              const Router = new VueRouter({
                scrollBehavior: () => ({ x: 0, y: 0 }),
                routes,
            
                // Leave these as is and change from quasar.conf.js instead!
                // quasar.conf.js -> build -> vueRouterMode
                // quasar.conf.js -> build -> publicPath
                mode: process.env.VUE_ROUTER_MODE,
                base: process.env.VUE_ROUTER_BASE
              })
            return Router
            }
            

            So, maybe you can make your routes array a function that return an array depending on what you want ?

            F 1 Reply Last reply Reply Quote 0
            • F
              folami @tof06 last edited by

              @tof06 please I would like to see an example of this

              T 1 Reply Last reply Reply Quote 0
              • T
                tof06 @folami last edited by

                @folami
                Well, I was thinking to something like this :

                // src/router/routes.js
                import { Platform } from 'quasar'
                const routes = (ssrContext) => {
                  const platform = process.env.SERVER ? Platform.parseSSR(ssrContext) : Platform
                  return [
                    { 
                      path: '/',
                      component: () => import('layouts/' + (platform.is.mobile ? 'mobileView.vue' : 'mainLayout.vue'))
                      // ...
                    }, // ...
                  ]
                }
                export default routes
                
                //src/router/index.js
                import Vue from 'vue'
                import VueRouter from 'vue-router'
                import routes from './routes'
                
                Vue.use(VueRouter)
                
                export default function ({ store, ssrContext}) {
                  const Router = new VueRouter({
                    routes: routes(ssrContext),
                    mode: process.env.VUE_ROUTER_MODE,
                    base: process.env.VUE_ROUTER_BASE
                  })
                  return router
                }
                

                Note that I didn’t test anything. I don’t know if it works, but, IMHO, it should 🙂

                F 2 Replies Last reply Reply Quote 0
                • F
                  folami @tof06 last edited by

                  @tof06 thanks, I will try it and let you know if it works

                  1 Reply Last reply Reply Quote 0
                  • F
                    folami @tof06 last edited by

                    @tof06 Sorry for late response, it works thanks so much

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