4

I am doing an app where I need to extract the CSS inside the style tag in a single file component in Vue JS.

So I have this basic template:

<template>
  <div class="wrapper">
    <button class="wrapper__button" @click="sendRequest()" click me></button>
  </div>
</template>

<script>
export default {
  name: 'HelloWorld',
  data () {
    return {
      msg: 'Welcome to Your Vue.js App'
    }
  },
  methods: {
    sendRequest () {
      console.log(document.getElementsByTagName("STYLE")[0].innerHTML)
      this.$http.post('http://localhost:3000/users/pdf', 'random string').then((response) => {
        console.log(response.data)
      })
    }
  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style>
.wrapper__button {
  background-color: yellow;
}
</style>

Is there a way I can get with Javascript all the data inside the styles in the vue component?

Thanks, guys

4
  • do you mean css styles that you want to grab inside a variable? Or you want to style html from component Commented Aug 20, 2018 at 14:27
  • i want to get all the css inside <style></style> Commented Aug 20, 2018 at 14:32
  • I'm curious why one needs to extract the style tag? The compiled one (if used scoped they will add [data...]), or which version?? I don't believe this is possible, or even and desired. Commented Dec 25, 2019 at 14:25
  • @Xan if my answer solves your bounty, please award the bounty manually, as the original author is seen 11 months ago the last time... Commented Dec 29, 2019 at 19:15

3 Answers 3

2
+50

There is, but you need to use CSS modules.

Vue Loader's documentation is more detailed about usage examples:

<style module>
.red {
  color: red;
}
.bold {
  font-weight: bold;
}
</style>

<template>
  <p :class="$style.red">
    This should be red
  </p>
</template>

<script>
export default {
  created () {
    console.log(this.$style.red)
    // -> "red_1VyoJ-uZ"
    // an identifier generated based on filename and className.
  }
}
</script>

Vue Loader's documentation also shows how to use both the Scoped CSS and CSS Modules with this kind of Webpack config:

{
  test: /\.css$/,
  oneOf: [
    // this matches `<style module>`
    {
      resourceQuery: /module/,
      use: [
        'vue-style-loader',
        {
          loader: 'css-loader',
          options: {
            modules: true,
            localIdentName: '[local]_[hash:base64:5]'
          }
        }
      ]
    },
    // this matches plain `<style>` or `<style scoped>`
    {
      use: [
        'vue-style-loader',
        'css-loader'
      ]
    }
  ]
}
Sign up to request clarification or add additional context in comments.

Comments

1

In jQuery, you can do

var color = $(".wrapper__button").css("background-color"));

to fetch all the css properties. (you will need to do some string manipulation)

var color = $(".wrapper__button").attr('style');

Here is a working example https://jsfiddle.net/kLpv30dz/

1 Comment

i want to get all the css inside <style></style>, not just the background-color
1

This answer is something different from vue, but you can extract the CSS using Javascript by using document.styleSheets

for (var i=0; i < document.styleSheets.length; i++){
  var styleSheet = document.styleSheets[i];
  console.log(styleSheet);
}
.wrapper{
  color: red;
}
<div class=".wrapper"></div>

2 Comments

this seems something i am looking for, but how can i distingue between diferent component styles to get the desired one if i have many component?
I don't think so beacuse it will give all the stylesheets used on the page, not component wise.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.