Will all the default values of vue components become invalid after they are introduced into prop?

I want to implement:

(:colorpropsp)

problems encountered:

getStyle()colorfont-size

Target:

1.stylegetStyle()colorfont-size()
2.colorcolorfont-sizegetStyle()()
3.colorfont-weight(font-size)()
4.font-weightfont-italic(:),()

here is a small example I wrote:

:)

<template>
    <div>
        <p :style="getStyle()">haha

</div> </template> <script> export default { name: "Test", props: { textStyle: Object }, methods: { getStyle() { return this.textStyle || { "color": "-sharp999", "font-size": "22px" } } } } </script>
Mar.13,2021

    getStyle() {
        return Object.assign({
            'color': '-sharp999',
            'font-size': '22px'
        },this.textStyle)
    }

I understand your problem is to merge the style attribute of the default setting with the externally passed attribute. If the two have the same attribute, the externally passed attribute will prevail, and if this attribute is not passed in externally, the default value will be used. For some properties that do not have a default value, they use the properties that are passed in externally.

The simpler way to

is to merge the two attributes directly with Object.assign . Also, I recommend that you use the computed calculation property instead of methods to define this, because the calculation attribute can calculate new values in real time based on external property changes.

<template>
    <div>
        <p :style="getStyle">haha

</div> </template> <script> export default { name: 'Test', props: { textStyle: Object }, computed: { getStyle() { const defaultStyle = { 'color': '-sharp999', 'font-size': '22px' } return Object.assign(defaultStyle, this.textStyle || {}) } } } </script>

what if it's a method

Menu