Home > Mobile >  How can I import styles for pc or for mobile?
How can I import styles for pc or for mobile?

Time:04-05

I have three folders: base, pc and mobile. I always connect index.scss from the first folder through the css section in nuxt.config.js.
To determine the type of device, I use @nuxtjs/device. And now I want to asynchronously import styles for mobile devices and for PCs.

I try to run this code in the default template, but a parsing error occurs.

mounted () {
  import('~/assets/scss/'   (this.$device.isMobile ? 'mobile' : 'pc')   '/index.scss')
}

Screenshot with an error

Update 1:

I also tried another code like this:

import Vue from 'vue'
import { Item as MenuItem } from '../interfaces/base/menu'
export default Vue.extend({
  name: 'DefaultLayout',
  data: (): any => ({
    menu: [] as Array<MenuItem>
  }),
  mounted () {
    if (this.$device.isMobile)
      import('../assets/scss/mobile/index.scss')
    else
      import('../assets/scss/pc/index.scss')
  }
})

And I got such errors: Update 1 errors

Even in VS Code hints there are no .scss files: VS Code suggestions

Update 2:

  async mounted () {
    if (process.client) {
      if (this.$device.isMobile)
        await import('~/assets/scss/mobile/index.scss')
      else
        await import('~/assets/scss/pc/index.scss')
    }
  }

Console errors: Console errors

CodePudding user response:

async mounted () {
  if (process.client) {
    if (this.$device.isMobile)
      await import('~/assets/scss/mobile/index.scss')
    else
      await import('~/assets/scss/pc/index.scss')
  }
}

should do the trick as explained in my answer here.

  • Related