VueJS-函数结果作为Prop的默认值

时间:2020-08-18 12:54:51

标签: vue.js uuid vue-props vue-class-components vue-property-decorator

将VueJS与类组件和vue-property-decorator一起使用,我正在尝试编写一个uuid Prop的Vue组件,默认情况下,该Prop默认为新的uuid(使用npm软件包uuid):

import {Component, Prop, Vue} from 'vue-property-decorator';
import * as uuid from 'uuid';

@Component
export default class BlockComponent extends Vue {
  @Prop({default: uuid.v1()}) uuid!: string;
}

,以便在实例化中提供服务器提供的uuid时,将其用于前端,如果未使用,则意味着它是一个新元素,并且应为该组件生成新的uuid。这段代码的问题在于,最终我会产生多个具有相同uuid的组件(但不是全部)。

1 个答案:

答案 0 :(得分:1)

根据documentation,您可以使用构造函数,因此可以传递一个函数。

@Prop(options: (PropOptions | Constructor[] | Constructor) = {})