组件中混入js,并进行对象样式的相关操作

64 篇文章 3 订阅
17 篇文章 0 订阅

一、在混入的js中可以操作组件的datacomputed

1.:style加入多个对象样式

这是一个 Diagram.vue组件
<div
      ref="content"
      class="my-go-diagram__content"
      :style="[contentStyle, myCursor]"
    ></div>
这是它的引入,以及computed
import factory from '../mixins/factory'

export default {
  name: 'Diagram',
  mixins: [
    factory(),// 混入
    layout,
    finder
  ],
  computed: {
	contentStyle () {
      const { left, top, bottom, right } = this.rect
      return {
        left: `${left}px`,
        top: `${top}px`,
        bottom: `${bottom}px`,
        right: `${right}px`
      }
    }
}

2.在factory中进行操作

这是factory.js
export default function (ClassName, defaultOptions, ref) {
  return {
    props: {
      options: [Object, Function],
      nodes: {
        type: Array,
        default() {
          return []
        }
      },
      links: {
        type: Array,
        default() {
          return []
        }
      },
      // modelData
      data: {
        type: Object,
        default() {
          return {}
        }
      },
      myStatus: {
        type: String,
        default: 'indicator'
      }
  },
    data() {
      return {
        loading: true,
        myCursor: 'cursor:Auto'
      }
    },
    methods: {
    	bind(diagram) {
		this.myCursor = {cursor: 'crosshair'}// 控制factory的js中的data,这个myCursor 是能够在组件中直接使用的
            console.log(this.contentStyle);
            Object.assign(this.contentStyle, {cursor: 'crosshair'})//给计算属性的对象添加一项
            this.contentStyle.cursor = undefined // 将计算属性对象的指定项删除
            this.myCursor = 'cursor:crosshair'
}
    }    

组件与混入js中的数据是共享的

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值