简体   繁体   中英

Vue - how can i load CSS and JS files in a component?

I'm very new to Vue and i created a navbar component that i want to load.

Here is my code:

component.vue

<template>
    <html>
        <head>
        ...
        <link href="https://fonts.googleapis.com/css?family=Gudea:400,700" rel="stylesheet">
        <link href="assets/plugins/bootstrap/css/bootstrap.min.css" rel="stylesheet">
        <link href="assets/plugins/font-awesome/css/font-awesome.min.css" rel="stylesheet">

        <!-- Theme Styles -->
        <link href="assets/css/flatifytheme.min.css" rel="stylesheet">
        <link href="assets/css/custom.css" rel="stylesheet">
        ...
        </head>
        <body>
         ...
        
         <script src="assets/plugins/jquery/jquery-3.1.0.min.js"></script>
         <script src="assets/plugins/bootstrap/js/bootstrap.min.js"></script>
         <script src="assets/plugins/jquery-slimscroll/jquery.slimscroll.min.js"></script>
         <script src="assets/plugins/waves/waves.min.js"></script>
         <script src="assets/plugins/uniform/js/jquery.uniform.standalone.js"></script>
         <script src="assets/plugins/switchery/switchery.min.js"></script>
         <script src="assets/plugins/pace/pace.min.js"></script>
         <script src="assets/js/flatifytheme.min.js"></script>

        </body>
    </html>
</template>

The assets directory is in the same folder as the component.

Now the problem is that i have a lot of <link href=''> and <scripts> tags that load js and css files of the theme i'm using for the navbar and i don't know where to put them in the component, so i keep getting the following error:

This relative module was not found:
* ./components/testComponent in ./src/main.js, ./node_modules/cache-loader/dist/cjs.js??ref--12-0!./node_modules/babel-loader/lib!./node_modules/cache-loader/dist/cjs.js??ref--0-0!./node_modules/vue-loader/lib??vue-loader-options!./src/App.vue?vue&type=script&lang=js&

What does this error mean? How can i fix it?

There are two ways of doing this:

  1. Either these are global files in your project
    • load the remote files (google fonts any http files) directly in your index.html
    • load the project files via the bundler in your main.js or App.vue
    • with import '@/assets/plugins/bootstrap/css/bootstrap.min.css' and so on...
  2. Or, these are local files that need to be loaded in specific components
    • load the remote files (google fonts or any http files) with vue-meta plugin which helps your create links, script and other HTML tags on-the-fly in the head tag https://vue-meta.nuxtjs.org/api/#link
    • load the project files with import '@/path/to/js/file' for JS files or for CSS:
<style>
@import '@/path/to/js/file';
</style>

Use @import in the style tag to import CSS-files.

@import './assets/plugins/font-awesome/css/font-awesome.min.css'

Also the paths to the files are incorrect, use ./ .

./assets/plugins/jquery/jquery-3.1.0.min.js

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM