Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to import css file from assets folder in nuxt.js

<template>
  <div class="container">
    <head>
      <link rel="stylesheet" href="~assets/css/style-light.css" />
      <link rel="stylesheet" href="~assets/css/login-light.css" />
    </head>
  </div>
</template>

Importing css like above results in this error

vue.runtime.esm.js:5717 GET http://localhost:3000/~assets/css/login-light.css net::ERR_ABORTED 404 (Not Found)

Is there really no other way loading css other than putting the whole css in the template?

like image 500
obliviousfella Avatar asked Feb 18 '20 10:02

obliviousfella


People also ask

Where do I put CSS files in nuxt?

Now about you doubt if you want to import the CSS as globally, the correct place is inside your nuxt. config. js, inside this file, you have a property called head, and inside the head we will configure all the imports. So, inside nuxt.

How do you use assets in nuxt?

From nuxt documentation: Inside your ‍‍ vue templates, if you need to link to your assets directory use ~/assets/your_image. png with a slash before assets.

How do I import Vue components into nuxt?

In order to use Vue plugins we need to create a Nuxt plugin and we do this by creating a . js file in the /plugins folder. We then paste in the code that we need to use the plugin. We import Vue and VTooltip and then tell Vue to use the VTooltip.

Where is body tag in nuxt?

It's available under the "head. boddyAttrs. class" attribute and you can refer below.

How to import CSS file in nuxt?

Now about you doubt if you want to import the CSS as globally, the correct place is inside your nuxt.config.js, inside this file, you have a property called head, and inside the head we will configure all the imports. So, inside nuxt.config.js find your head session, and then create new property called css, some thing like this:

How to serve strong assets in nuxt?

By default, Nuxt uses vue-loader, file-loader and url-loader webpack loaders for strong assets serving. You can also use the static directory for static assets.

How to add a HTML head in nuxt?

The first thing you need to know is, you can’t declare a html head inside any place, neither in yours tamplate, neither in yours components, neither in yours pages, neither in nowhere. The correct place is inside your nuxt.config.js, inside this file, you have a property called head, and inside this head you will configure your imports.

Does nuxt use Vue-loader?

By default, Nuxt uses vue-loader, file-loader and url-loader webpack loaders for strong assets serving. You can also use the static directory for static assets. vue-loader automatically processes your style and template files with css-loader and the Vue template compiler out of the box.


2 Answers

The first thing you need to know is, you can't declare a html head inside any place, neither in yours tamplate, neither in yours components, neither in yours pages, neither in nowhere.

Keep in mind that you can't use a html tags for this, you will use a json schema.

take a look https://nuxtjs.org/guide/configuration for more detailed explanations.

Now about you doubt if you want to import the CSS as globally, the correct place is inside your nuxt.config.js, inside this file, you have a property called head, and inside the head we will configure all the imports.

So, inside nuxt.config.js find your head session, and then create new property called css, some thing like this:

   head: {
     css: [
       '~/assets/style/app.styl',
       '~/assets/style/main.css'
     ],
   }
   ...

Another way, is import your css directly inside your component, for this you can do some thing like this:

        <style scoped>
        @import '~/assets/style/main.css';
        </style>
    OR
        <style scoped src="@/assets/styles/mystyles.css">
        </style>

In Nuxt, you will need a CSS loader instaled in your application too, so have sure you had intalled a "stylus" and "stylus-loader" in your app.

like image 188
Henrique Van Klaveren Avatar answered Nov 15 '22 07:11

Henrique Van Klaveren


try to impot your css files in script like this :

<script>
import "@/assets/css/style-light.css";
import "@/assets/css/login-light.css";

/// 

</script>

EDIT: changed ~ to @

like image 31
Adrian A Avatar answered Nov 15 '22 06:11

Adrian A