Vue 之 混入

2020-04-24  本文已影响0人  my木子

1、混入

2、全局混入

3、混入和普通组件

1、普通组件引用

2、 混入组件引用

// tableMixin.js
export default {
  data() {
    return {
      readonly: false,
      title: null,
      selectData: '',
      checked: false,
      addDialog: false,
      page: {
        pageSize: 10,
        pageNum: 1,
        total: 0
      },
      row: null,
      tableHeight: null
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.windowResize()
    })
    window.onresize = () => this.windowResize()
  },
  methods: {
    // 监听窗口变化
    windowResize() {
      if (document.querySelector('.table-search') && document.querySelector('.fenye')) {
        const table_search = document.querySelector('.table-search').offsetHeight
        const fenye = document.querySelector('.fenye').offsetHeight
        this.tableHeight = `calc(100% - ${table_search + fenye}px)`
      }
    },
    // 当选择项发生变化时会触发
    handleSelectionChange(val) {
      this.selectData = val
      if (this.selectData.length === this.tableData.length) {
        this.checked = true
      } else {
        this.checked = false
      }
    },
    // 全选
    toggleSelection(checked) {
      this.$refs.multipleTable.toggleAllSelection(checked)
    },
    search() {
      this.page.pageNum = 1
      this.list()
    },
    add() {
      this.title = '新增'
      this.addDialog = true
    },
    handleClickInfo(row) {
      this.title = '查看'
      this.row = row
      this.readonly = true
      this.addDialog = true
    },
    handleClickModify(row) {
      this.title = '修改'
      this.row = row
      this.addDialog = true
    },
    cancel() {
      this.handleClose()
    },
    submit() {
      this.handleClose()
      this.list()
    },
    handleClose() {
      this.addDialog = false
      this.row = null
      this.readonly = false
    },
    // 删除
    del() {
      if (this.selectData.length === 0) {
        this.$message({
          message: '请先选择'
        })
        return
      }
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        var num = []
        this.selectData.forEach((item, index) => {
          num.push(item.id)
        })
        this._delt(num)
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消操作'
        })
      })
    },
    // 启用
    Enable() {
      if (this.selectData.length === 0) {
        this.$message({
          message: '请先选择'
        })
        return
      }
      this.$confirm('此操作即将启动, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        var num = []
        this.selectData.forEach((item, index) => {
          num.push(item.id)
        })
        this._enable(num)
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消操作'
        })
      })
    },
    // 禁用
    Disable() {
      if (this.selectData.length === 0) {
        this.$message({
          message: '请先选择'
        })
        return
      }
      this.$confirm('此操作即将启动, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        var num = []
        this.selectData.forEach((item, index) => {
          num.push(item.id)
        })
        this._disable(num)
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消操作'
        })
      })
    }
  }
}

// index.vue
<script>
import { list, delt } from '@/api/device'
import tableMixin from '@/mixins/tableMixin'

export default {
  mixins: [tableMixin],
  data() {
    return {
    }
  },
  methods: {
    // 删除
    _delt(listId) {
      delt(listId).then(response => {
        this.$message({
          message: response.msg,
          type: 'success'
        })
        this._list()  // 刷新列表
      })
    }
  }
}
</script>
上一篇 下一篇

猜你喜欢

热点阅读