Home Web Front-end JS Tutorial Two ways for Vue single-page application to reference separate style files

Two ways for Vue single-page application to reference separate style files

May 28, 2018 am 10:32 AM
Quote style

This article will introduce to you how to reference a separate style file in a Vue single-page application. This article takes the css file as an example and introduces it to you in two ways in great detail. Friends who need it can refer to it

Problem description

For .vue files, they are also composed of three parts: structure, behavior, and style. There is a scoped attribute in the style part, that is The current page is valid. When there are many styles in the style tag or there are duplications between .vue files, it always feels that it does not look neat enough, so you need to introduce some common styles. Let’s first talk about how to introduce separate style files. Here we take CSS files as an example, and then talk about the division of style files in my project

Introducing separate style files

Method 1

Introduce static resources into main.js. This method makes the style file shared by components in the project

// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
import axios from 'axios'
// 此处引入静态资源
require('./assets/css/style.css')
/* eslint-disable no-new */
new Vue({
 el: '#app',
 router,
 template: '<App/>',
 components: { App }
})
Copy after login

Method 2

Introduce style files into a certain .vue

<template>
  ...
</template>

<script>
  export default {
    name: "test"
  }
</script>
<style scoped>
 @import "style.css";
</style>
Copy after login

The file organization format is as follows:

Applications in the project

In my project, both methods are applied. The public styles are referenced in the first method, and the styles of each module in the project are referenced in the second method. In each module, It contains a style file, which is used to store the styles needed in this module. At this time, you need to be more flexible. If there are relatively few styles, or only a certain vue file will use it, you can still write the css style directly in the .vue file. in the style tag.

The above is what I compiled for everyone. I hope it will be helpful to everyone in the future.

Related articles:

Implementation of vue’s keep-alive cache function

Collect url and href of front-end interview questions , src

Use vue-route’s beforeEach to implement the navigation guard (verify login before routing jump) function

The above is the detailed content of Two ways for Vue single-page application to reference separate style files. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

macOS: How to change the color of desktop widgets macOS: How to change the color of desktop widgets Oct 07, 2023 am 08:17 AM

macOS: How to change the color of desktop widgets

How to use block quotes in Apple Notes How to use block quotes in Apple Notes Oct 12, 2023 pm 11:49 PM

How to use block quotes in Apple Notes

C++ compilation error: undefined reference, how to solve it? C++ compilation error: undefined reference, how to solve it? Aug 21, 2023 pm 08:52 PM

C++ compilation error: undefined reference, how to solve it?

Guide to solving misalignment of WordPress web pages Guide to solving misalignment of WordPress web pages Mar 05, 2024 pm 01:12 PM

Guide to solving misalignment of WordPress web pages

CSS web background image design: create various background image styles and effects CSS web background image design: create various background image styles and effects Nov 18, 2023 am 08:38 AM

CSS web background image design: create various background image styles and effects

How to use C++ reference and pointer parameter passing? How to use C++ reference and pointer parameter passing? Apr 12, 2024 pm 10:21 PM

How to use C++ reference and pointer parameter passing?

What are the benefits of C++ functions returning reference types? What are the benefits of C++ functions returning reference types? Apr 20, 2024 pm 09:12 PM

What are the benefits of C++ functions returning reference types?

In-depth analysis of pointers and references in C++ to optimize memory usage In-depth analysis of pointers and references in C++ to optimize memory usage Jun 02, 2024 pm 07:50 PM

In-depth analysis of pointers and references in C++ to optimize memory usage

See all articles