01.vue 中 less 的简单使用

使用 less 写一个弹框组件

在用 vue create vueless 时选择 css 预处理

Alert.vue

<template>
  <div class="alert">
    <div class="zhezhao">
      <div class="alertMain">
        <div class="title">
          温馨提示:{{title}}
        </div>
        <div class="content">
          {{content}}
        </div>
        <div class="btnList">
          <div class="btn cancle" @click="cancleEvent">取消</div>
          <div class="btn comfirm" @click="comfirmEvent">确认</div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
  export default {
    name: "Alert",
    props: ['title', 'content', 'cancleEvent', 'comfirmEvent']
  }
</script>

// less 的 css 写法
<style lang="less">
  @theme-color: orange;
  @default-size: 18px;
  @font-color: #fff;
  @zzOpacity: 0.6;

  .fullScreen() {
    width: 100vw;
    height: 100vh;
    display: flex;
  }

  .defaultPadding() {
    padding: 10px;
  }

  .button (@padding: 20px) {
    padding: @padding;
    text-align: center;
  }

  .alert {
    .fullScreen();
    .zhezhao {
      .fullScreen();
      background: rgba(0, 0, 0, @zzOpacity);
      justify-content: center;
      align-items: center;
    }
    .alertMain {
      width: 600px;
      height: 400px;
      background-color: #fff;
      display: flex;
      flex-direction: column;
      position: relative;
      .title {
        font-size: @default-size;
        height: 50px;
        background-color: @theme-color;
        color: @font-color;
        .defaultPadding();
        text-align: center;
        line-height: 50px;
      }
      .content {
        .defaultPadding();
        font-size: @default-size;
      }
      .btnList {
        position: absolute;
        left: 0;
        bottom: 0;
        display: flex;
        width: 100%;
      }
      .btn {
        .button();
        flex: 1;
      }
      .cancle {
        background-color: pink;
      }
      .comfirm {
        background-color: yellowgreen;
      }
    }
  }
</style>

App.vue

<template>
  <div id="app">
    <Alert v-if="isAlert" :title="title" :content="content" :cancleEvent="cancleEvent" :comfirmEvent="comfirmEvent"></Alert>
  </div>
</template>

<script>
import HelloWorld from './components/HelloWorld.vue'
import Alert from "./components/Alert";

export default {
  name: 'App',
  components: {
    HelloWorld,
    Alert
  },
  data() {
    return {
      title: '跳转页面',
      content: '是否要跳转至百度',
      isAlert: true
    }
  },
  methods: {
    cancleEvent: function () {
      this.isAlert = false
    },
    comfirmEvent: function () {
      location.href = 'http://www.baidu.com'
    }
  }
}
</script>

<style lang="less">
* {
  margin: 0;
  padding: 0;
}
</style>

在这里插入图片描述

©️2020 CSDN 皮肤主题: 书香水墨 设计师:CSDN官方博客 返回首页