0

これが私が使用するコードです。

<template>
  <div class="button-layout" :style="`margin: ${margin}; text-align: ${align};`">
    <component
      :is="buttonComponent"
      v-for="(button, index) in buttons.filter(btn => btn.url)"
      :key="button.label"
      :label="button.label"
      v-scroll-to="button.url"
      :style="`margin-left: ${index === 0 ? '' : space};`" />
    <component
      :is="buttonComponent"
      v-for="(button, index) in buttons.filter(btn => !btn.url)"
      :key="button.label"
      :label="button.label"
      :type="button.type"
      :style="`margin-left: ${index === 0 ? '' : space};`" />
  </div>
</template>

<script>
export default {
  name: "ButtonLayout",
  components: {  },
  props: {
    button: String,
    margin: String,
    align: String,
    space: String,
    buttons: Array
  },
  computed: {
    buttonComponent() {
      return () => import(`./button/${this.button}`)
    }
  }
};
</script>

オブジェクト構造のこれら 2 つのリストを使用でき、正常に動作します。

[
    { url: '#video', label: lang.video },
    { url: '#info', label: lang.info }
]
[
    { type: 'reset', label: lang.clear },
    { type: 'submit', label: lang.send }
]

コードを繰り返すのは好きではないので、リストの最初のオブジェクトに基づいて属性typeを動的に追加しようとしましたが、機能しません。v-scroll-toそれを達成するための最良の方法は何ですか?(以下のコードを参照)

<template>
  <div class="button-layout" :style="`margin: ${margin}; text-align: ${align};`">
    <component
      :is="buttonComponent"
      v-for="(button, index) in buttons"
      :key="button.label"
      :label="button.label"
      v-bind:[optionalDirective.directive]="button[optionalDirective.key]"
      :style="`margin-left: ${index === 0 ? '' : space};`" />
  </div>
</template>

<script>
export default {
  name: "ButtonLayout",
  components: {  },
  props: {
    button: String,
    margin: String,
    align: String,
    space: String,
    buttons: Array
  },
  computed: {
    buttonComponent() {
      return () => import(`./button/${this.button}`)
    },
    optionalDirective(){
      if(this.buttons[0].url) {
        return {
          directive: 'v-scroll-to',
          key: 'url'
        }
      } else {
        return {
          directive: 'type',
          key: 'type'
        }
      }
    }
  }
};
</script>
4

2 に答える 2