Vuetify,如何设置默认道具

时间:2019-02-20 17:12:53

标签: javascript vue.js vuetify.js

我已经开始使用Vuetify,但我正在寻找一种方法来修改某些组件上的默认道具。

有没有办法做到这一点?

即而不需要经常写:

<v-layout wrap></v-layout>

我可以使布局的默认默认包装属性为true吗?

1 个答案:

答案 0 :(得分:1)

遵循这些原则,但是请注意,如果您不熟悉vue.js,则必须阅读以下内容:

相关文档:vue mixinvue extends

js

// some already existing component, you need to get it somehow
// most likely via `import <something-to-import>`
let theExternalComponent = {
  props: { wrap: { default: false, type: Boolean } },
  template: "<li>wrap:{{wrap}}</li>"
};
// this simulates the global registration
Vue.component("v-some-external-component", theExternalComponent);

// -- lets start --

// lets extend that component - and overwrite the default prop for wrap
let extendedExternalwithOtherDefaults = {
  extends: theExternalComponent,
  mixins: [{ props: { wrap: { default: true } } }],
};

var app = new Vue({
  el: "#app",
  components: { "v-my-customized-component": extendedExternalwithOtherDefaults }
});

html(实际上是pug,但这在这里无关紧要)

div(id="app")
  ul
    v-some-external-component

    v-some-external-component(wrap)

    v-my-customized-component
    // now defaults to wrap:true

    v-my-customized-component(:wrap="false") 
    // you can still set the wrap to false if required

输出

wrap:false
wrap:true
wrap:true
wrap:false

codepen:https://codepen.io/anon/pen/MLxbEW