如何在Vue项目中为所有组件设置默认的lang="stylus"?
Set Default
lang="stylus" for All Vue Components Great question! Having to type lang="stylus" in every single component's <style> block is such a tedious chore—let's fix that once and for all. The approach depends on whether you're using Vue CLI or Vite (the two most common Vue project setups), so I'll cover both:
For Vue CLI Projects
- First, make sure you have
stylusandstylus-loaderinstalled (if you haven't already):npm install stylus stylus-loader --save-dev # or yarn add stylus stylus-loader --dev - Locate or create a
vue.config.jsfile in your project root (this is where we'll tweak the Vue loader settings). - Add the following configuration to override the default style language:
module.exports = { chainWebpack: config => { config.module .rule('vue') .use('vue-loader') .tap(options => { // Update the compiler options to set stylus as the default style lang options.compilerOptions = { ...options.compilerOptions, style: { lang: 'stylus' } } return options }) } } - Restart your dev server, and now every
<style>block in your components will default to Stylus—no need to addlang="stylus"anymore! If you ever need to use a different preprocessor for a specific component, just manually add thelangattribute (likelang="scss") to override the default.
For Vite Projects
- First, install
stylus(Vite handles the loader internally, so no need for a separate loader package):npm install stylus --save-dev # or yarn add stylus --dev - Open your
vite.config.jsfile in the project root. - Modify the Vue plugin configuration to set Stylus as the default style language:
import { defineConfig } from 'vite' import vue from '@vitejs/plugin-vue' export default defineConfig({ plugins: [ vue({ template: { compilerOptions: { style: { lang: 'stylus' } } } }) ] }) - Restart your Vite dev server, and you're good to go! Just like with Vue CLI, you can still override the default for individual components by specifying a different
langattribute.
内容的提问来源于stack exchange,提问作者trusktr
相关产品推荐
相关产品推荐

