【问题标题】:returning vuejs component from method从方法返回 vuejs 组件
【发布时间】:2018-10-22 14:42:19
【问题描述】:

我对 vue.js 还是很陌生,所以我什至不确定我正在尝试做的事情是否可行。我正在开发一个 Matrix 组件,然后我可以在其他应用程序中使用它。我的问题是我希望 getIdentity() 方法返回一个新的 Matrix 组件,其中传入了一个数组

export default {
  name: 'Matrix',
  props: {
    initRows: Number,
    initColumns: Number,
    initValues: Array
  },
  data () {
    return {
      editable: true,
      contents: [],
      rows: 1,
      columns: 1
    }
  },
  created () {
    ...
  },
  methods: {
    addColumn () {...},
    removeColumn (column) {...},
    addRow () {...},
    removeRow (row) {...},
    getRREF () {...},
    getInverse () {...},
    getIdentity () {
      if (this.rows === this.columns) {
        let tempMtx = [];
        for (let row = 0; row < this.rows; row++) {
          tempMtx[row] = [];
          for (let col = 0; col < this.columns; col++) {
            tempMtx[row][col] = row === col ? 1 : 0;
          }
        }
        return new Matrix({propsData: {initValues: tempMtx}}); // This is the part I can't figure out
      } else {
        throw new Error('Rows and Columns must be equal to get the identity matrix');
      }
    }
  }
}

【问题讨论】:

  • 你想如何使用从那个方法返回的组件?
  • 好吧,我希望它是一个 Matrix 对象/组件,然后我可以向用户显示或执行操作,例如使用 getInverse() 方法或将两个 Matrix 对象连接在一起。我知道在 Java 中可以从类的方法中调用类的构造函数,我希望在这里得到类似的行为。我尝试执行“let MatrixClass = vue.extends(this)”,它返回了一个 vue 组件,但没有任何应该包含在其中的数据。
  • 我认为拥有 Matrix 对象/类和呈现它的组件(接受 Matrix 作为道具)可能会更好。但听起来你让自己工作得很好!

标签: javascript node.js vue.js


【解决方案1】:

this article 的帮助下,我最终弄明白了。除了没有从另一个文件中导入它,我只是定义了let MatrixConstructor = vue.extend(this.a);

【讨论】:

  • 构造函数定义也需要在export语句之后
猜你喜欢
  • 2018-10-15
  • 2020-01-10
  • 2018-03-19
  • 2018-11-26
  • 2018-12-13
  • 1970-01-01
  • 2019-02-15
  • 1970-01-01
相关资源
最近更新 更多