Vue.js VueJS-函数的结果作为道具的默认值

Vue.js VueJS-函数的结果作为道具的默认值,vue.js,uuid,vue-props,vue-class-components,vue-property-decorator,Vue.js,Uuid,Vue Props,Vue Class Components,Vue Property Decorator,将VueJS与类组件和vue属性装饰器一起使用我正在尝试使用uuid道具编写vue组件,该道具在未提供时默认为新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.

将VueJS与类组件和
vue属性装饰器一起使用
我正在尝试使用
uuid
道具编写vue组件,该道具在未提供时默认为新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生成了多个组件(但不是所有组件)。

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

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


使用函数()=>uuid.v1()太棒了!谢谢我不知道我可以传递一个函数,该函数返回一个字符串作为字符串属性的默认值。最后我做了这样的事情:
{default:uuid.v1}
然后works@Estradiaz想写一个简短的答案,这样我就可以解决这个问题了吗?