v-if on style block












0














Is it possible to use v-if on style block, like so?



<style lang="scss" v-if="pinkTheme">
.ac-textfield
{
background: hotpink;
}
</style>


I tried this and it's not working (the v-if directive is ignored). I know that I could do this:



<div class="ac-textfield" v-style="{ background: pinkTheme ? 'hotpink' : false }"></div>


However, this will quickly become difficult to maintain if I want to modulate more than one style using the value of pinkTheme. Right now, I'm using CSS selectors to achieve the desired effect:



<template>
<div class="ac-textfield" :class="{ pinkTheme }">
</div>
</template>

<style lang="scss">
.ac-textfield
{
//...
}

.ac-textfield.pink-theme
{
background: hotpink;
}
</style>


However, I don't like this solution very much (I want to achieve greater separation between my themes, and I want the browser to only have to load one theme at a time). Is there any way to make something like the first code block work?










share|improve this question



























    0














    Is it possible to use v-if on style block, like so?



    <style lang="scss" v-if="pinkTheme">
    .ac-textfield
    {
    background: hotpink;
    }
    </style>


    I tried this and it's not working (the v-if directive is ignored). I know that I could do this:



    <div class="ac-textfield" v-style="{ background: pinkTheme ? 'hotpink' : false }"></div>


    However, this will quickly become difficult to maintain if I want to modulate more than one style using the value of pinkTheme. Right now, I'm using CSS selectors to achieve the desired effect:



    <template>
    <div class="ac-textfield" :class="{ pinkTheme }">
    </div>
    </template>

    <style lang="scss">
    .ac-textfield
    {
    //...
    }

    .ac-textfield.pink-theme
    {
    background: hotpink;
    }
    </style>


    However, I don't like this solution very much (I want to achieve greater separation between my themes, and I want the browser to only have to load one theme at a time). Is there any way to make something like the first code block work?










    share|improve this question

























      0












      0








      0







      Is it possible to use v-if on style block, like so?



      <style lang="scss" v-if="pinkTheme">
      .ac-textfield
      {
      background: hotpink;
      }
      </style>


      I tried this and it's not working (the v-if directive is ignored). I know that I could do this:



      <div class="ac-textfield" v-style="{ background: pinkTheme ? 'hotpink' : false }"></div>


      However, this will quickly become difficult to maintain if I want to modulate more than one style using the value of pinkTheme. Right now, I'm using CSS selectors to achieve the desired effect:



      <template>
      <div class="ac-textfield" :class="{ pinkTheme }">
      </div>
      </template>

      <style lang="scss">
      .ac-textfield
      {
      //...
      }

      .ac-textfield.pink-theme
      {
      background: hotpink;
      }
      </style>


      However, I don't like this solution very much (I want to achieve greater separation between my themes, and I want the browser to only have to load one theme at a time). Is there any way to make something like the first code block work?










      share|improve this question













      Is it possible to use v-if on style block, like so?



      <style lang="scss" v-if="pinkTheme">
      .ac-textfield
      {
      background: hotpink;
      }
      </style>


      I tried this and it's not working (the v-if directive is ignored). I know that I could do this:



      <div class="ac-textfield" v-style="{ background: pinkTheme ? 'hotpink' : false }"></div>


      However, this will quickly become difficult to maintain if I want to modulate more than one style using the value of pinkTheme. Right now, I'm using CSS selectors to achieve the desired effect:



      <template>
      <div class="ac-textfield" :class="{ pinkTheme }">
      </div>
      </template>

      <style lang="scss">
      .ac-textfield
      {
      //...
      }

      .ac-textfield.pink-theme
      {
      background: hotpink;
      }
      </style>


      However, I don't like this solution very much (I want to achieve greater separation between my themes, and I want the browser to only have to load one theme at a time). Is there any way to make something like the first code block work?







      vue.js vue-component






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 23 at 2:48









      laptou

      1,694821




      1,694821
























          2 Answers
          2






          active

          oldest

          votes


















          1














          That cannot work as scss must be compiled to css - and this is done at build-time. v-if, though, is executed/interpreted at run-time only.



          What would kind of work, is to include the style block in the template part of your single file vue component, but note that you can only use CSS, not SCSS, in there. That has the obvious downside of getting rendered alongside each of the component instances.



          Your approach of using conditional CSS classes actually is the correct approach, so you should definitely stick to that.






          share|improve this answer





















          • I thought it might work, because each style block is compiled into a webpack module, right? What stops Vue.js from simply loading and unloading these modules based on the condition? I realise that doing this directly would introduce a dependency on webpack, but doesn't the idea make sense?
            – laptou
            Nov 23 at 3:39










          • Anyway, thank you for the answer.
            – laptou
            Nov 23 at 3:39



















          0














          v-if is used for conditional html rendering and v-if accept the getter function that should return either true or false. You can't use v-if with style.






          share|improve this answer





















            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53440094%2fv-if-on-style-block%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            1














            That cannot work as scss must be compiled to css - and this is done at build-time. v-if, though, is executed/interpreted at run-time only.



            What would kind of work, is to include the style block in the template part of your single file vue component, but note that you can only use CSS, not SCSS, in there. That has the obvious downside of getting rendered alongside each of the component instances.



            Your approach of using conditional CSS classes actually is the correct approach, so you should definitely stick to that.






            share|improve this answer





















            • I thought it might work, because each style block is compiled into a webpack module, right? What stops Vue.js from simply loading and unloading these modules based on the condition? I realise that doing this directly would introduce a dependency on webpack, but doesn't the idea make sense?
              – laptou
              Nov 23 at 3:39










            • Anyway, thank you for the answer.
              – laptou
              Nov 23 at 3:39
















            1














            That cannot work as scss must be compiled to css - and this is done at build-time. v-if, though, is executed/interpreted at run-time only.



            What would kind of work, is to include the style block in the template part of your single file vue component, but note that you can only use CSS, not SCSS, in there. That has the obvious downside of getting rendered alongside each of the component instances.



            Your approach of using conditional CSS classes actually is the correct approach, so you should definitely stick to that.






            share|improve this answer





















            • I thought it might work, because each style block is compiled into a webpack module, right? What stops Vue.js from simply loading and unloading these modules based on the condition? I realise that doing this directly would introduce a dependency on webpack, but doesn't the idea make sense?
              – laptou
              Nov 23 at 3:39










            • Anyway, thank you for the answer.
              – laptou
              Nov 23 at 3:39














            1












            1








            1






            That cannot work as scss must be compiled to css - and this is done at build-time. v-if, though, is executed/interpreted at run-time only.



            What would kind of work, is to include the style block in the template part of your single file vue component, but note that you can only use CSS, not SCSS, in there. That has the obvious downside of getting rendered alongside each of the component instances.



            Your approach of using conditional CSS classes actually is the correct approach, so you should definitely stick to that.






            share|improve this answer












            That cannot work as scss must be compiled to css - and this is done at build-time. v-if, though, is executed/interpreted at run-time only.



            What would kind of work, is to include the style block in the template part of your single file vue component, but note that you can only use CSS, not SCSS, in there. That has the obvious downside of getting rendered alongside each of the component instances.



            Your approach of using conditional CSS classes actually is the correct approach, so you should definitely stick to that.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 23 at 2:54









            connexo

            20.3k73554




            20.3k73554












            • I thought it might work, because each style block is compiled into a webpack module, right? What stops Vue.js from simply loading and unloading these modules based on the condition? I realise that doing this directly would introduce a dependency on webpack, but doesn't the idea make sense?
              – laptou
              Nov 23 at 3:39










            • Anyway, thank you for the answer.
              – laptou
              Nov 23 at 3:39


















            • I thought it might work, because each style block is compiled into a webpack module, right? What stops Vue.js from simply loading and unloading these modules based on the condition? I realise that doing this directly would introduce a dependency on webpack, but doesn't the idea make sense?
              – laptou
              Nov 23 at 3:39










            • Anyway, thank you for the answer.
              – laptou
              Nov 23 at 3:39
















            I thought it might work, because each style block is compiled into a webpack module, right? What stops Vue.js from simply loading and unloading these modules based on the condition? I realise that doing this directly would introduce a dependency on webpack, but doesn't the idea make sense?
            – laptou
            Nov 23 at 3:39




            I thought it might work, because each style block is compiled into a webpack module, right? What stops Vue.js from simply loading and unloading these modules based on the condition? I realise that doing this directly would introduce a dependency on webpack, but doesn't the idea make sense?
            – laptou
            Nov 23 at 3:39












            Anyway, thank you for the answer.
            – laptou
            Nov 23 at 3:39




            Anyway, thank you for the answer.
            – laptou
            Nov 23 at 3:39













            0














            v-if is used for conditional html rendering and v-if accept the getter function that should return either true or false. You can't use v-if with style.






            share|improve this answer


























              0














              v-if is used for conditional html rendering and v-if accept the getter function that should return either true or false. You can't use v-if with style.






              share|improve this answer
























                0












                0








                0






                v-if is used for conditional html rendering and v-if accept the getter function that should return either true or false. You can't use v-if with style.






                share|improve this answer












                v-if is used for conditional html rendering and v-if accept the getter function that should return either true or false. You can't use v-if with style.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 23 at 2:55









                Atul Bansode

                11




                11






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.





                    Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                    Please pay close attention to the following guidance:


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53440094%2fv-if-on-style-block%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

                    Calculate evaluation metrics using cross_val_predict sklearn

                    Insert data from modal to MySQL (multiple modal on website)