美文网首页
Vue风格指南

Vue风格指南

作者: Ivy_study | 来源:发表于2017-12-07 10:32 被阅读0次

    a.优先级A -- 私有属性名

    在插件、混入等扩展中始终为自定义的私有属性使用 $_ 前缀。并附带一个命名空间以回避和其它作者的冲突 (比如 $yourPluginName)。

    var myGreatMixin = {
      // ...
      methods: {
        $_myGreatMixin_update: function () {
          // ...
        }
      }
    }
    

    b.优先级A -- 为组件样式设置作用域

    对于应用来说,顶级App组件和布局组件中的样式可以是全局的,但是其它所有组件都应该是有作用域的。

    这条规则只和单文件组件有关。你不一定要使用scoped特性。设置作用域也可以通过CSS Modules,那是一个基于 class 的类似BEM的策略,当然你也可以使用其它的库或约定。

    不管怎样,对于组件库,我们应该更倾向于选用基于 class 的策略而不是scoped特性。

    这让覆写内部样式更容易:使用了常人可理解的 class 名称且没有太高的选择器优先级,而且不太会导致冲突。

    <template>
      <button class="button button-close">X</button>
    </template>
    
    <!-- 使用 `scoped` 特性 -->
    <style scoped>
    .button {
      border: none;
      border-radius: 2px;
    }
    
    .button-close {
      background-color: red;
    }
    </style>
    <template>
      <button :class="[$style.button, $style.buttonClose]">X</button>
    </template>
    
    <!-- 使用 CSS Modules -->
    <style module>
    .button {
      border: none;
      border-radius: 2px;
    }
    
    .buttonClose {
      background-color: red;
    }
    </style>
    <template>
      <button class="c-Button c-Button--close">X</button>
    </template>
    
    <!-- 使用 BEM 约定 -->
    <style>
    .c-Button {
      border: none;
      border-radius: 2px;
    }
    
    .c-Button--close {
      background-color: red;
    }
    </style>
    

    c.优先级B -- 基础组件名

    应用特定样式和约定的基础组件 (也就是展示类的、无逻辑的或无状态的组件) 应该全部以一个特定的前缀开头,比如 Base、App 或 V。

    components/
    |- BaseButton.vue
    |- BaseTable.vue
    |- BaseIcon.vue
    
    components/
    |- AppButton.vue
    |- AppTable.vue
    |- AppIcon.vue
    
    components/
    |- VButton.vue
    |- VTable.vue
    |- VIcon.vue
    

    相关文章

      网友评论

          本文标题:Vue风格指南

          本文链接:https://www.haomeiwen.com/subject/pmxdixtx.html