Make include css and js files with vuejs

Asked

Viewed 1,659 times

0

I’m studying the implementation of Windows and vuejs and using as template the admin Lte. My question is: no index.html vuejs, I put all the base css and js files. But when the page requires a specific css and or js file, how do this include in the vuejs Component? Before I was doing everything in one place, now I separated the backend of the frontend. Not always will a js file have an export, especially in these cases web templates. What’s more, there is also the need to import css files to specific pages.

1 answer

0


Just use the require Based on Commonjs

For example when importing bootstrap you simply install it and pull with require.

require('bootstrap');

to pull the css

require('bootstrap/dist/css/bootstrap.min.css');

To pull any other file, just climb into the component root folder (src) and search...

require('./css/style.css');

You can use loaders like css-Loader and style-Loader, just install and add the url-Loader

{
   test: /\.(woff|woff2|ttf|svg|eot)$/,
   loader: 'url-loader'
}

To import js, still recommend using the import, just declare a class and use it on the page with the new, example...

class.js

export class Classe{

    constructor(nome){
        this.nome = nome;
    }

}

main.js

import {Classe} from './classe';
new Vue({
    el: '#app',
    data: {
    nome : new Classe('stackOverflow')
}
  • But when is a js of some template? For example, I am using adminlte as tests, some pages require specific js but which do not necessarily return a class but have several functions for a certain thing on that page. How to import this kind of js?

  • Well, I’ve never tried with js inline, but require wouldn’t work?

  • Felipe, the times I tried didn’t work out. I end up loading ALL css and js in the index of Vue, which ends up generating an unnecessary extra load in the application.

  • Sorry for the delay, but if you are using Webpack you need some settings to export external js, if I am not mistaken, you need loaders like https://webpack.js.org/loaders/script-Loader/ and also manage the distribution of Chunks to the pages that will actually use certain files, thus eliminating the loading of all at once.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.