Home > Web Front-end > Front-end Q&A > vue-cli3 packaging style misalignment

vue-cli3 packaging style misalignment

WBOY
Release: 2023-05-24 10:46:06
Original
704 people have browsed it

With the widespread use of Vue.js in front-end development, many developers have begun to use Vue CLI 3 to manage their Vue projects and use Webpack to package projects for production environments. However, when using Vue CLI 3 packaging, we may encounter some style misalignment problems. This article will explore how to solve these problems.

  1. Check dependency versions

When packaging with Vue CLI 3 and Webpack, make sure that the versions of all dependencies are up to date and not lower than the Vue CLI requirements. You can check the Vue CLI version using the following command:

vue --version
Copy after login

and make sure the installed version is 3.x.x.

At the same time, when using npm or yarn to install dependencies, make sure to use the latest versions of packages, especially packages related to CSS and styles, such as sass-loader, css-loader, etc.

  1. Check CSS preprocessor configuration

Vue CLI 3 uses scss as the CSS preprocessor by default. If you use another CSS preprocessor, such as less or stylus, and it is not set up correctly in the project configuration, it may cause misaligned styles. Therefore, make sure that the CSS preprocessor is configured correctly in the vue.config.js file, for example:

module.exports = {
  css: {
    loaderOptions: {
      less: {
        // 使用less的变量
        modifyVars: {
          // 或者您想使用jQuery和Bootstrap等的cdn链接
          'jquery':'jquery',
          'moment':'moment',
          'i18n':'vue-i18n',
          'bootstrap':'https://cdn.bootcss.com/bootstrap/4.0.0/css/bootstrap.min.css',
        }
      }
    }
  }
}
Copy after login

In this example, we have added an option called less and used modifyVars to change the Less variable Passed into our component file. By properly configuring the CSS preprocessor in Vue CLI 3, we can solve the style misalignment problem.

  1. Check whether to use global styles

In the Vue project, we can use global styles to apply to components. If we use our own CSS selector in the global style, it may cause style misalignment problems, because the Vue component may be included in a DOM element that has a CSS selector defined.

To solve this problem, we can use CSS scoping to limit the style of the component. To do this, in the Vue single-file component, we can use the