11

Is there any vue plugin that can allows us to use template variable in side <style/> tag in Single File Components for e.g.

<template>

    <div>{{ display }}</div>

</template>


<script>

    export default {
        data(){
            return { display: 'block' }
        }
    }

</script>


<style>
    body {
        display: {{ display }}
    }
</style>

Any better way/plugin to do this??

I already have known about :style and :class

7
  • You would do this using CSS classes designed for this purpose, not by directly manipulating CSS rules. Commented Mar 9, 2018 at 14:17
  • Yes I got you! but when we want @media css manipulation then its become issue! Commented Mar 9, 2018 at 14:19
  • I don't see how that use case would require any different approach. Commented Mar 9, 2018 at 14:20
  • @connexo think like you want different image on desktop and different image on mobile! Commented Mar 9, 2018 at 14:24
  • You'd do that with CSS... No Javascript involved at all. Commented Mar 9, 2018 at 14:28

2 Answers 2

3

I think there is no way to access the Vue model (data layer) inside <Style> in the current version of Vue. Vue only control the DOM tree but not help you work around on CSSOM.

You are probably using WebPack or other bundler and writing modularized component in single file where you have <template>, <script>, and <style>. Your bundler will have a way to convert <template> into Vue render function, but style here is purely css and will be bundled into a single css file.

If you want to set a styling value dynamically that controlled by your component, you need to find a way to inject them in run-time, i.e. using the Vue version of styled-component (as they came from React originally). But you will lose the caching ability, unless you also save the value in localStorage.

Sign up to request clarification or add additional context in comments.

Comments

-1

https://v2.vuejs.org/v2/guide/class-and-style.html#Object-Syntax-1

You can put whatever you want in your style attribute by binding inline styles:

<div v-bind:style="{ color: activeColor, fontSize: fontSize + 'px' }"></div>

1 Comment

"I already have known about :style and :class" said on the Question, please don't put a generic answer

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.