Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot work with CSS Modules #21

Closed
banrikun opened this issue Jul 3, 2019 · 4 comments · Fixed by #420
Closed

Cannot work with CSS Modules #21

banrikun opened this issue Jul 3, 2019 · 4 comments · Fixed by #420

Comments

@banrikun
Copy link

banrikun commented Jul 3, 2019

<script>
  export default {
    props: {
      themeName: String
    },

    computed: {
      $theme () {
        return this[this.themeName]
      }
    }
  }
</script>

<style module="$a">
  .wrapper {
    background: #000;
  }
</style>

<style module="$b">
  .wrapper {
    background: #fff;
  }
</style>

setup (props, context) cannot work with CSS Modules, because context does not has property $a or $b.

@posva posva added the enhancement New feature or request label Jul 31, 2019
@mgdodge
Copy link

mgdodge commented Oct 28, 2019

Also, when using without custom inject names, context does not have the property $style:

<script>
  export default {
    setup(props, context) {
      // Get undefined instead of { wrapper: <random-string> }
      return context.$style;
    }
  }
</script>

<style module>
  .wrapper {
    background: #000;
  }
</style>

@mgdodge
Copy link

mgdodge commented Oct 29, 2019

As a workaround, depending on your usage, it is possible to extract $a $b and $style from the onBeforeMount hook. For example:

<script>
  import { onBeforeMount } from '@vue/composition-api';
  export default {
    setup() {
      const css = { $a, $b, $style };
      onBeforeMount(function(){
        ({ $a: css.$a, $b: css.$b, $style: css.$style } = this);
      });
      // css.$a, css.$b, css.$style are now available for use in computed properties, etc.
      return {
        // no need to return $a/$b/$styles, added to instance by vue-loader
      };
    }
  }
</script>

<style module>
  .wrapper {
    background: #000;
  }
</style>

<style module="$a">
  .wrapper {
    background: #ccc;
  }
</style>

<style module="$b">
  .wrapper {
    background: #fff;
  }
</style>

@yoyo930021
Copy link
Member

yoyo930021 commented Feb 10, 2020

My workaround

import { getCurrentInstance } from '@vue/composition-api';

export default {
  setup () {
     const { $style } = getCurrentInstance()!
  }
}

@nbgraham
Copy link

nbgraham commented Oct 2, 2021

See useCssModule

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants