条件编译 - 图1
条件编译 - 图2

视图层

  1. <template>
  2. <!-- #ifdef H5 --> // 仅仅只在 H5 页面显示
  3. <view class="btn-box" v-bind:style="{color:color}" v-on:click="open">
  4. <slot></slot>
  5. </view>
  6. <!-- #endif -->
  7. </template>
  8. ---------------------- #ifndef
  9. <template>
  10. <!-- #ifndef H5 --> // 仅不在 H5 页面显示
  11. <view class="btn-box" v-bind:style="{color:color}" v-on:click="open">
  12. <slot></slot>
  13. </view>
  14. <!-- #endif -->
  15. </template>

业务逻辑层

  1. <script>
  2. export default {
  3. props: {
  4. color: {
  5. type: String,
  6. dafult: '#000'
  7. }
  8. },
  9. data() {
  10. return {}
  11. },
  12. methods: {
  13. // #ifdef H5 // 仅仅只在 H5 页面运行
  14. open() {
  15. console.log(' btn 被点击了')
  16. this.$emit('AAA', this.color)
  17. }
  18. // #endif
  19. }
  20. }
  21. </script>

样式层

  1. <style>
  2. /* #ifdef H5 */
  3. .btn-box {
  4. width: 200px;
  5. height: 200px;
  6. border: red solid 1px;
  7. line-height: 100px;
  8. text-align: center;
  9. }
  10. /* #endif */
  11. </style>