JIYIK CN >

Current Location:Home > Learning > WEB FRONT-END > Vue >

How to introduce style files in Vue

Author:JIYIK Last Updated:2025/02/26 Views:

Vue is a popular front-end framework that allows developers to build single-page applications in a componentized way. During the development process, we usually need to introduce style files to beautify the page. This article will introduce several ways to introduce style files in Vue.

  1. Import the style file into the component

In Vue, each component can have its own style. We can <style>import the style file in the component's tag. For example:

<template>
  <div class="hello-world">Hello World!</div>
</template>

<script>
export default {
  name: 'HelloWorld',
};
</script>

<style src="./HelloWorld.css"></style>

In the above example, we HelloWorld.vueimport HelloWorld.cssthe style file into the component. Use srcthe property to specify the path of the style file.

  1. Introduce the global style file in App.vue

If we want to share styles across the entire application, we can App.vueimport a global style file in . For example:

<template>
  <div id="app">
    <router-view />
  </div>
</template>

<script>
export default {
  name: 'App',
};
</script>

<style src="./assets/global.css"></style>

In the example above, we App.vueimported global.cssthe style file in . This style file will be applied to the entire application.

  1. Using CSS Preprocessors

In Vue, we can also use CSS preprocessors to write styles. Common CSS preprocessors include Sass, Less, and Stylus. Using CSS preprocessors allows us to write complex styles more conveniently and improves the maintainability of the code.

Vue supports using Sass and Less in single-file components. If we want to use Sass in a component, we can <style>add lang="scss"the attribute to the tag. For example:

<template>
  <div class="hello-world">Hello World!</div>
</template>

<script>
export default {
  name: 'HelloWorld',
};
</script>

<style lang="scss">
@import "./HelloWorld.scss";

.hello-world {
  color: $primary-color;
}
</style>

In the example above, we use Sass in the component and import HelloWorld.scssthe style file. We also define .hello-worldthe class name and use $primary-colorthe variable to set the text color.

Summarize

The methods for introducing style files in Vue are:

  1. Import the style file into the component
  2. Introduce the global style file in App.vue
  3. Using CSS Preprocessors

Using these methods allows us to manage styles more conveniently and improve the maintainability of the code. In actual development, we can choose different methods to introduce style files according to actual conditions.

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

Webpack packages ES6 and CommonJs mixed React

Publish Date:2025/03/02 Views:179 Category:React

This article mainly introduces how to use webpack to package and compile React mixed with ES6 and CommonJs. It is a process of upgrading the React environment.

How to scroll to the top or bottom of the page in Vue.js

Publish Date:2025/03/01 Views:96 Category:Vue

Vue.js is a popular front-end framework that helps developers build efficient and maintainable applications. In Vue.js, scrolling the page to the top or bottom is a common requirement. In this article, we will introduce how to implement this

Display element or text on mouse hover in vue

Publish Date:2025/03/01 Views:179 Category:Vue

Vue.js is a popular JavaScript framework that makes web application development easier and more efficient. In this tutorial, we will learn how to use Vue.js to display an element or text on mouse hover. This tutorial will cover the following

What is the use of the immediate property of watch in Vue?

Publish Date:2025/03/01 Views:67 Category:Vue

In Vue, watch is a way to perform asynchronous tasks or trigger responsive dependencies when data changes. In most cases, watch will be delayed by default. This means that the watch will only be triggered when the value being monitored chang

Setting up checkbox functionality in Vue

Publish Date:2025/03/01 Views:186 Category:Vue

In Vue, checkboxes are a very common interactive component that allows users to select multiple options. This article will introduce how to set up checkbox functionality in Vue and provide some practical examples.

What happens if a child component changes the data in props in Vue?

Publish Date:2025/03/01 Views:140 Category:Vue

In Vue, when a child component changes the data in props, it will cause the responsiveness of the parent component and other child components to change. First, you need to understand that props is a way to pass data from a parent component t

How to refresh the page in Vue

Publish Date:2025/03/01 Views:91 Category:Vue

Vue is a popular JavaScript framework that provides many convenient tools and methods for building web applications. In Vue, page updates are usually achieved through data binding and responsive systems. But sometimes you need to refresh the

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial