0

Using Vue Router to load the components.

Note that i am not using webpack or vue-loader.

Here is a sample component that get loaded by vue - router

export default {
    name: 'Abc',
    template:'<div>Now .. i can't add style element here :(. So where should i add it </div>',
    data(){
        return {
             message:' new message'
        }
    },
}

Where can i add css styles. I don't care about css scoped if it is not possible.

Don't want to use render functions ( https://v2.vuejs.org/v2/guide/render-function#The-Data-Object-In-Depth ) .. as creating the dom structure would kill me

2
  • Without webpack you cannot modularize CSS as mentioned in the Vue docs. Most of the vue conponents out there provide a CSS file to be included in the head tag fo such cases. So effectively you have two choices a separate CSS file or inline-css in your template code. Commented Apr 23, 2019 at 11:03
  • yeah .. for now i have used the head css file,, Commented Apr 29, 2019 at 10:47

3 Answers 3

1
+25

As is stated in this answer, you can't define separate CSS in a Vue component using something like css like you can define HTML using template.

That being said, there are a few ways you can define CSS for specific components/elements:


Scoped CSS

You can define a style to be scoped to a component:

App.vue

<template>
  <div id="app">
    <RedComponent/>
    <NotRedComponent/>
  </div>
</template>

<script>
import RedComponent from "./components/RedComponent";
import NotRedComponent from "./components/NotRedComponent";

export default {
  components: {
    RedComponent,
    NotRedComponent
  }
};
</script>

<style>
#app {
  font-family: "Avenir", Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  margin-top: 60px;
}
</style>

RedComponent.vue

<script>
export default {
  template: "<div>I am red</div>"
};
</script>

<style scoped>
div {
  color: red;
}
</style>

NotRedComponent.vue

<script>
export default {
  template: "<div>I am not red</div>"
};
</script>

See this live here


CSS Classes and IDs

You can give elements classes and IDs in order to select them with CSS, and just have a separate CSS file. Note: this is not unique to Vue.

App.vue

<script>
export default {
  name: "App",
  template: '<div><p class="red">I am red</p><p>I am not red</p></div>'
};
</script>

index.css

.red {
  color: red;
}

See this live here

You can reference this index.css file from anywhere (within reason) - for example, in my live demo it is referenced from within index.html itself (something like <link rel="stylesheet" type="text/css" href="index.css" /> within the <head> tag will do).


Inline styles

For this, using backticks (`) rather than quotes will make your life easier. Another benefit of using backticks is the fact that you can span your template over multiple lines.

App.vue

<script>
export default {
  name: "App",
  template: `<div>
    <p style="color: red">I am red</p>
    <p>I am not red</p>
  </div>`
};
</script>

See this live here


Personally, I've never found a use-case where scoped CSS won't do the trick, even with vue-router, but those are some alternative options if you can't use it for whatever reason.

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

10 Comments

for RedComponent.vue : what would be the return type : application/javascript or text/html
are you sure it was without webpack
I think scoped does use webpack actually, as Vue needs to add specific data tags to the individual elements in order to scope them properly when it compiles... the other two options should work fine without webpack though.
I don't understand your question about return type - RedComponent.vue is a Vue component, just kept in a different file.
yeah .. different file for RedComponent.vue .. i tried both application/javascript and text/html MIME for it, both failed to import in the script .. like import {z} from 'a/b';
|
0

You can use any CSS library like Bootstrap, Bulma etc, and add css classes directly in template like the following.

In head section of your page add bootstrap reference link and that's it.

template: `<div class="container">Boostrap</div>`

2 Comments

cannot add to head section of the page to reduce page load time
Definitely, it is not recommended to include in head section. I was telling about how it works.
0

If you need to use .vue files without webpack (vue-loader) you can have a look at vue3-sfc-loader.

vue3-sfc-loader
Load .vue files dynamically at runtime from your html/js. No node.js environment, no (webpack) build step needed.

(disclamer: author here)

Comments

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.