【发布时间】:2020-06-04 00:51:23
【问题描述】:
我制作了一个自定义的 Vue 组件并通过 npm 发布。我的组件在我的主项目中按预期工作,但在当前使用情况下,用户需要发送太多道具才能有效地使用我的组件并且没有重复代码。
我将组件安装为
//index.js in component folder
import MyComponent from "./MyComponent.vue";
export default {
install(Vue, options) {
Vue.component("my-component", MyComponent);
}
};
并在我的项目中注册为
//main.js in project folder
import Vue from 'vue'
import MyComponent from 'my-component'
Vue.use(MyComponent)
在我的.vue 文件中使用我的自定义组件:
<my-component v-model="myData" prop1="prop1" prop2="prop2" prop3="prop3"></my-component>
由于该道具的使用看起来如此不整洁并且导致我自己的代码重复过多,我想在选项中注册该道具并通过我的组件。但无法弄清楚如何做到这一点。
【问题讨论】:
-
v-bind="{prop1,prop2,prop3}并在组件中props: ['prop']参见 doc