Some tips for using less in Vue projects

Some tips for using less in Vue projects

Preface

The beautiful web pages we can see are all carefully designed by UI and built by front-end engineers. If you want a web page to have a cool style, you need to use CSS to process it, which will inevitably result in a lot of repeated and redundant code. At this time, style preprocessors such as less, sass, and scss appear, which greatly simplifies the CSS code and improves development efficiency. Today, let’s follow this article to see how to use less syntax in Vue projects to penetrate effects and mixins~

1. Style penetration

The structure of the vue project consists of three parts: template, script, and style. The lang attribute in style determines the syntax of the style. Setting the scoped attribute can prevent the style of the current page from polluting other pages.

1. What is pattern penetration?

The style you set overrides the original style

2. How to use?

When we use a packaged public component, we are not satisfied with the original style provided by the component and want to adjust the style. We cannot modify the styles in public components, so we need to use style penetration to help us solve this problem.

Writing in vue2

The code is as follows (example):

<style lang="less" scoped>
	/deep/ a {
            text-decoration: none;
	}
</style>
<style lang="less" scoped>
	::v-deep a {
            text-decoration: none;
	}
</style>

Writing in Vue3

<style lang="less" scoped>
	:deep(a) {
            text-decoration: none;
	}
</style>

2. Mixing

1. What is a mixin?

Similar to the function in js, it extracts the repeated code in the style and can be introduced multiple times when used.

2. How to use?

definition

The code is as follows (example):

<style lang="less" scoped>
    .abc() {
        color: skyblue
        }
</style>

use

<style lang="less" scoped>
    p {
        font-size: 20px;
        .abc();
      }
</style>

3. Less automatic import

1. Benefits of automated import

You can extract frequently appearing style files and put them into a less file.

Then you can use it directly where you need it, without manually importing the file

2. How to achieve it?

  • Use the style-resoures-loader plug-in of vue-cli to automatically inject it into the style tag of each vue component

Run vue add style-resources-loader in the terminal in the project root directory to add a vue-cli plug-in

Note: A query will pop up in the terminal window. Type y and select less.

  • After the installation is complete, the vue.config.js file will be automatically generated. Just add the address of the less file that needs to be automatically imported in the configuration.

The code is as follows (example):

const path = require('path')

module.exports = {
  pluginOptions: {
    'style-resources-loader': {
      preProcessor: 'less',
      patterns:
        // Configure which files need to be automatically imported path.join(__dirname, './src/xx/xx.less')
      ]
    }
  }
}

Summarize

This is the end of this article about some tips for using less in vue projects. For more information about using less in vue, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • 22 Vue optimization tips (project practical)
  • Vue.js performance optimization N tips (worth collecting)
  • Summary of practical skills commonly used in Vue projects
  • Summary of 10 advanced tips for Vue Router
  • 8 tips for Vue that you will learn after reading it
  • Sharing tips on using vue element and nuxt
  • Summary of common routines and techniques in Vue development
  • A brief discussion on the use of Vue functional components
  • 6 tips for writing better v-for loops in Vue.js
  • 25 Vue Tips You Must Know

<<:  VMware virtual machine to establish HTTP service steps analysis

>>:  How to gracefully and safely shut down the MySQL process

Recommend

How to turn a jar package into a docker container

How to turn a jar package into a docker container...

Common methods of Vue componentization: component value transfer and communication

Related knowledge points Passing values ​​from pa...

7 native JS error types you should know

Table of contents Overview 1. RangeError 2. Refer...

How to use JS WebSocket to implement simple chat

Table of contents Short Polling Long-Polling WebS...

Steps to split and compress CSS with webpack and import it with link

Let's take a look at the code file structure ...

Install CentOS 7 on VMware14 Graphic Tutorial

Introduction to CentOS CentOS is an enterprise-cl...

Detailed steps to install MySql 5.7.21 in Linux

Preface The most widely used database in Linux is...

mysql5.5.28 installation tutorial is super detailed!

mysql5.5.28 installation tutorial for your refere...

js to implement file upload style details

Table of contents 1. Overview 2. Parameters for c...

Solution to forgetting the MYSQL database password under MAC

Quick solution for forgetting MYSQL database pass...

Background gradient animation effect made by css3

Achieve results Implementation Code html <h1 c...

Analysis of several reasons why Iframe should be used less

The following graph shows how time-consuming it is...

How to define data examples in Vue

Preface In the development process, defining vari...