1

var myComponent= Vue.extend({
    template:
    `
    <div class="container">
    </div>
    `
    ,
    props: [],
    components: {}
    ,
    data() {
        return {
        }
    },

    methods: {
    }
})



I have a component above that is created using the Vue.extend. It takes in data, methods and other things that are all locally scoped. I'm wondering if I can have CSS that is locally scoped to this component within the object passed to vue.extend()

I'm not using nodejs (using django) so I don't think I can use the recommended syntax within .vue elements (If I'm mistaken and I can use .vue files and the below syntax please let me know):

<style scoped>
/* local styles */
</style>

1 Answer 1

1

You may include this in your template string property:

template: `
    <div class="container">

        ...

        <style scoped> 
            /*your css*/
        </style>

    </div>
    `

This style tag will attach all the css to your parent container element as root, so it won't affect your entire document.

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

2 Comments

And by the way, this is not in the official documentation, if your classes aren't being properly built in production mode, you should check how to assing styles to your components using this.$style inside your code/vue instance
this within template solution does not work when using non-node

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.