自己手写一个带插槽slot的

104 篇文章 0 订阅
13 篇文章 0 订阅
<template>
  <van-overlay :show.sync="show" @click="close">
    <div @click.stop class="x-dialog">
      <div class="header">{{title}}</div>
      <slot></slot>
      <div class="footer"><slot name="footer"></slot></div>
    </div>
  </van-overlay>
</template>
<script>
export default {
  props: {
    show: {
      type: Boolean,
      default: false
    },
    title: {
      type: String,
      default: '提示'
    }
  },
  data () {
    return {
    }
  },
  mounted () {
  },
  computed: {
  },
  methods: {
    close () {
      this.$emit('update:show', false)
    }
  }
}
</script>
<style lang="scss" scoped>
.x-dialog{
  background-color: #fff;
  position: fixed;
  top: 35%;
  left: 50%;
  transform: translateX(-50%);
  width: 6.2rem;
  border-radius: 0.2rem;
  .header{
    text-align: center;
    line-height: 0.42rem;
    font-size: 0.3rem;
    color: #333;
    font-weight: 600;
    padding: 0.3rem 0;
  }
  .footer{
    height: 1.1rem;
    border-top: 1px solid #EEEEEE;
  }
}
</style>

  • 上面的组件全局注册了的
import Vue from 'vue'

const requireComponent = require.context(
  // 组件的相对路径
  './',
  // 是否查询其子目录
  true,
  // 匹配基础组件文件名的正则表达式, 这里匹配文件名为xxx.vue格式
  /index.vue$/
)
requireComponent.keys().forEach(fileName => {
  // 获取组件配置
  const componentConfig = requireComponent(fileName)
  // 剥去文件名开头的 `./` 和结尾的扩展名 eg: ./food-header.vue -> foodHeader
  const componentName = fileName.replace(/^\.\//, '').replace(/\/index.vue$/, '')
  Vue.component(
    componentName,
    // 如果这个组件选项是通过 `export default` 导出的,
    // 那么就会优先使用 `.default`,否则回退到使用模块的根。
    componentConfig.default || componentConfig
  )
})

  • 其他地方使用
<template>
  <div>
    <x-dialog :title="title" :show.sync="show" style="z-index:100">
      <van-form ref="dialogForm">
        <div class="x-dialog-form">
          <van-field
            v-model="remark"
            name="name"
            label=""
            rows="3"
            type="textarea"
            show-word-limit
            maxlength="1000"
            :placeholder="'请填写' + title"
            :rules="[{ required: true, message: '请填写' + title }]"
          />
          <van-field name="checkbox" label="通知方式" input-align="right">
            <template #input>
              <van-checkbox v-model="checkbox" disabled shape="square">系统消息</van-checkbox>
            </template>
          </van-field>
        </div>
      </van-form>
      <template slot="footer">
        <p class="x-dialog-button">
          <span class="button" @click="show = false">取消</span>
          <span class="button sure" @click="onSubmit">确定</span>
        </p>
      </template>
    </x-dialog>
  </div>
</template>
<script>
export default {
  name: 'onePop',
  data () {
    return {
      show: false,
      remark: '',
      checkbox: true
    }
  },
  props: {
    title: {
      type: String,
      default: () => ''
    }
  },
  mounted () {
  },
  computed: {
  },
  methods: {
    openDia () {
      this.show = true
      this.$refs.dialogForm.resetValidation()
    },
    onSubmit (values) {
      this.$refs.dialogForm.submit()
    }
  }
}
</script>
<style lang="scss" scoped>
.x-dialog-form{
  padding: 0 0.6rem;
  margin-bottom: 0.32rem;
}
.x-dialog-button{
  height: 1rem;
  .button{
    width: 50%;
    float: left;
    text-align: center;
    line-height: 1.1rem;
    font-size: 0.3rem;
    color: #999;
    border: none;
    &.sure{
      color: #4F9FFA;
    }
  }
}
</style>

  • 常用的main.js
import Vue from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
import Vant from 'vant'

import Global from '@/base/global'

import 'vant/lib/index.css'
import echarts from 'echarts'

import '@/components/index'
import '@/icons/'
import '@/base/rem'

import '@/styles/index.scss'
Vue.prototype.$echarts = echarts

Vue.prototype.Global = new Global(Vue)
Vue.use(Vant)
Vue.config.productionTip = false

new Vue({
  router,
  store,
  render: h => h(App)
}).$mount('#app')

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值