How to include css files in Vue 2

后端 未结 4 1606
囚心锁ツ
囚心锁ツ 2020-12-01 03:00

I\'m new to vue js and trying to learn this. I installed a fresh new version of vue webpack in my system. I\'m having a css, js and images of this a theme template which I w

相关标签:
4条回答
  • 2020-12-01 03:06

    If you want to append this css file to header you can do it using mounted() function of the vue file. See the example.
    Note: Assume you can access the css file as http://www.yoursite/assets/styles/vendor.css in the browser.

    mounted() {
            let style = document.createElement('link');
            style.type = "text/css";
            style.rel = "stylesheet";
            style.href = '/assets/styles/vendor.css';
            document.head.appendChild(style);
        }
    
    0 讨论(0)
  • 2020-12-01 03:12

    Try using the @ symbol before the url string. Import your css in the following manner:

    import Vue from 'vue'
    
    require('@/assets/styles/main.css')
    

    In your App.vue file you can do this to import a css file in the style tag

    <template>
      <div>
      </div>
    </template>
    
    <style scoped src="@/assets/styles/mystyles.css">
    </style>
    
    0 讨论(0)
  • 2020-12-01 03:16

    You can import the css file on App.vue, inside the style tag.

    <style>
      @import './assets/styles/yourstyles.css';
    </style>
    

    Also, make sure you have the right loaders installed, if you need any.

    0 讨论(0)
  • 2020-12-01 03:25

    As you can see, the import command did work but is showing errors because it tried to locate the resources in vendor.css and couldn't find them

    You should also upload your project structure and ensure that there aren't any path issues. Also, you could include the css file in the index.html or the Component template and webpack loader would extract it when built

    0 讨论(0)
提交回复
热议问题