[关闭]
@Wangww0925 2020-08-03T02:52:58.000000Z 字数 2691 阅读 261

stylus 使用【grunt将styl转css、vue使用styl】

Vue_CLI css预处理器_stylus


下载demo

打开test.html页面


使用grunt将styl转css

参考.styl 转 .css

注意: 文件目录(路径以根目录开始)

1、目录结构

  1. stylus_demo
  2. |-| src
  3. |--| css
  4. |---| test.css (使用grunt生成的)
  5. |--| stylus
  6. |---| test.styl (内容根据实际来)
  7. |--| test.html
  8. |-| Gruntfile.js (grunt配置文件)
  9. |-| package.json
  10. |-| package-lock.json

2、文件内容

test.styl

  1. /**********************************继承************************************/
  2. .positionCenter
  3. position:absolute
  4. top:50%
  5. left:50%
  6. vendor('transform',translate(-50%,-50%))
  7. /**********************************定义函数********************************/
  8. /*
  9. * vendor('border-radius',5px)
  10. * vendor('box-shadow',0px 0px 1px 2px #333)
  11. * vendor('transform',translate(-50%,-50%))
  12. */
  13. vendor(prop, n)
  14. -webkit-{prop}:n
  15. -moz-{prop}:n
  16. -ms-{prop}:n
  17. -o-{prop}:n
  18. {prop}:n
  19. /**********************************主体内容********************************/
  20. .positionTop
  21. @extend .positionCenter
  22. width:100px
  23. height:100px
  24. background-color:red
  25. color:#fff
  26. text-align:center
  27. line-height:100px
  28. .div
  29. line-height:30px
  30. color:red
  31. &:hover
  32. color:blue

test.html

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Document</title>
  6. <link rel="stylesheet" href="./css/test.css">
  7. </head>
  8. <body>
  9. <div class="positionTop">定位</div>
  10. <div class="div">11111111111</div>
  11. </body>
  12. </html>

3、根目录 新建 Gruntfile.js 文件

  1. module.exports = function(grunt) {
  2. grunt.initConfig({ // 任务配置,所有插件的配置信息
  3. pkg: grunt.file.readJSON('package.json'),
  4. stylus:{
  5. build: {
  6. options: {
  7. linenos: false,
  8. compress: true
  9. },
  10. files: [{
  11. 'src/css/test.css': ['src/stylus/test.styl'] // 将test.styl转成test.css
  12. }]
  13. }
  14. },
  15. watch: { // watch插件的配置信息
  16. another: {
  17. files: ['src/css/*','src/stylus/*.styl'],
  18. tasks: ['stylus'],
  19. options: {
  20. livereload: 1337
  21. }
  22. }
  23. }
  24. });
  25. // 告诉grunt我们将使用插件
  26. grunt.loadNpmTasks('grunt-contrib-watch');
  27. grunt.loadNpmTasks('grunt-contrib-stylus');
  28. // 告诉grunt当我们在终端中输入grunt时需要做些什么 根据情况使用
  29. grunt.registerTask('default', ['watch']); // 监听.styl文件更改,并转换
  30. };

3、下载依赖

  1. & npm init // 创建项目
  2. & npm install grunt // 下载grunt
  3. & npm install grunt-contrib-stylus // .styl 转 .css
  4. & npm install grunt-contrib-watch // 监听指定的.styl

4、运行Gruntfile.js,执行命令grunt

执行命令之后会发现多了 src/css/test.css文件

5、打开test.html页面

效果



Vue中使用stylus

1、安装stylus:

  1. npm install stylus --save-dev
  2. npm install stylus-loader --save-dev

2、引入

方法一: 在.vue文件中使用stylus标签

例: 在style标签加上 lang="stylus"

  1. <style scoped lang="stylus">
  2. .avatar
  3. float: left;
  4. width: 300px;
  5. img
  6. width: 60px;
  7. height: 60px;
  8. display: inline-block;
  9. border-radius: 30px;
  10. </style>

方法二: 在.vue文件中引用.styl文件

  1. <style lang="stylus">
  2. @import "~@/assets/stylus/common.styl";
  3. </style>

vue补充(内部style调用外部.styl文件)

assets/stylus/base.styl

  1. p(n)
  2. padding: n;

.vue文件

  1. <template>
  2. <div id="contentBg>stylus使用</div>
  3. </template>
  4. <style lang="stylus">
  5. @import "~@/assets/stylus/base.styl";
  6. #contentBg
  7. p(50px 30px);
  8. </style>
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注