vue3 composition api mixin


mixinをvue 3コンビネーションAPIで置き換える

  • 悩み状況
    複数ページで使用する保険の種類、保険会社、請負業者選択ボックスのオプション名を1つのファイルで管理したいです.これは以前もmixinによって解決されていたので,この方法を用いて解決しようと試みた.しかし、コンビネーションapiではどのように書かれていますか?

  • vue 2 option api mixin用例

    // MyMixin.js
    export default {
      data: () => ({
        mySharedDataProperty: null
      }),
      methods: {
        mySharedMethod () { ... }
      }
    }
    // ConsumingComponent.js
    import MyMixin from "./MyMixin.js";
    
    export default {
      mixins: [MyMixin],
      data: () => ({
        myLocalDataProperty: null
      }),
      methods: {
        myLocalMethod () { ... }
      }
    }

    1.name話題を繰り返す!

    const mixin = {
      data: () => ({
        myProp: null
      })
    }
    
    export default {
      mixins: [mixin],
      data: () => ({
        // same name!
        myProp: null
      })
    }

    2.隠れた依存ホットスポット!

    const countDownMixin = {
      data() {
        return {
          countDown: 10,
          interval: null,
        };
      },
      computed: {
        isDone: function () {
          return this.countDown <= 0;
        },
      },
      methods: {
        decrement() {
          if (this.countDown > 0) this.countDown -= 1;
        },
      },
      mounted: function () {
        this.interval = setInterval(this.decrement, 1000);
      },
      unmounted: function () {
        clearInterval(this.interval);
      },
    };
    
    export default countDownMixin;
    - countDown을 20초로 바꾸고 싶다면...
    <template>
      <div>
      <h1 v-if="!isDone">Countdown: {{ countDown }} seconds</h1>
      <h1 v-else>Hello World</h1>
      </div>
    </template>
    
    <script>
    import countDownMixin from "./countDownMixin";
    export default {
      name: "HelloWorld",
      mixins: [countDownMixin],
      data(){
      return {countDown:20}
      }
    };
    </script>
    - composition api 방식
    import { ref, computed, onMounted, onUnmounted, readonly } from "vue";
    
    export default function useCountDown(countDownDuration = 10) {
      const countDown = ref(countDownDuration);
      const interval = ref(null);
      const decrement = () => {
        if (countDown.value > 0) countDown.value -= 1;
      };
      const isDone = computed(() => {
        return countDown.value <= 0;
      });
      onMounted(() => {
        interval.value = setInterval(decrement, 1000);
      });
      onUnmounted(() => {
        clearInterval(interval.value);
      });
    
      return { countDown:readonly(countDown), isDone };
    }
    - composition api 방식
    <template>
      <div>
      <h1 v-if="!isDone">Countdown: {{ countDown }} seconds</h1>
      <h1 v-else>Hello World</h1>
      </div>
    </template>
    
    <script>
    import useCountDown from "./useCountDown";
    export default {
      name: "HelloWorld",
      setup(){
      const {countDown, isDone} = useCountDown(20);
      return {countDown, isDone};
      }
    };
    </script>
    暗示の依存性もnameの繰り返しから始まるのでしょうか….
    リファレンスリンク
  • How the Vue Composition API Replaces Vue Mixins
  • implicit dependency