V
V
villager1232022-01-22 16:30:48
Node.js
villager123, 2022-01-22 16:30:48

How to correctly include a Vue component depending on an environment variable?

Hello.
There is a Vue application that is the same for both web and mobile. That is, I can assemble it both for publishing as a site and for use as a mobile webview application.
During the build, for both, I use an environment variable, and already in the code, depending on its value, I include certain components. Example:

{
    path: '/chat',
    name: 'chats',
    component: isWeb() ? Messages : ChatListMobile,
    children: [
      {
        path: ':id',
        name: 'chat',
        component: () => import('./inc/MessagesBody'),
        meta: {
          auth,
        },
      },
    ],
    meta: {
      middleware: [
        auth,
      ],
      animation: true,
    },
  },


isweb is:
export function isWeb() {
  return process.env.VUE_APP_PLATFORM === 'web';
}


And during development, everything is fine. But when I build the application, it doesn't work - there is an error in the console in the chunk, they say

Uncaught (in promise) TypeError: Object(...) is not a function
.
I tried to fight it in every possible way - I made a variable, wrapped it in promises, hardcoded isWeb as true, and so on.
When I scored and simply commented out all such conditions in the router, a similar problem appeared in the template components (there, according to the same condition, html blocks are shown and hidden).

I have already tried a lot - added to vue.config.js :
configureWebpack: {
    resolve: {
      alias: {
        'vue$': 'vue/dist/vue.js',
      },
    },
  },

- so that the version with the compiler gets into the assembly, but this does not work.
What's the matter, what to do, tell me. I don't know where to dig anymore. Is this an issue with the environment variable? Or is it necessary to build the application somehow differently (it works during development)?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question