How do I extend another VueJS component in a single-file component? (ES6 vue-loader)

后端 未结 5 852
迷失自我
迷失自我 2020-12-14 14:47

I am using vue-loader (http://vuejs.github.io/vue-loader/start/spec.html) to construct my *.vue single-file components, but I am having trouble with the process

5条回答
  •  情歌与酒
    2020-12-14 15:16

    As of Vue 3, the Composition API is introduced, making it very easy to share state across components.

    Rather than defining your component with properties on the component definition object e.g. data, computed, methods, etc, the Composition API allows you to instead create a setup function where you declare and return these.

    An example:

    file: useCounter.js

    import { reactive, computed } from "vue";
    
    export default function {
      const state = reactive({
        count: 0,
        double: computed(() => state.count * 2)
      });
    
      function increment() {
        state.count++
      }
    
      return {
        count,
        double,
        increment
      }
    }
    

    Now the counter feature can be seamlessly introduced into any Vue component using it's setup function:

    file: MyComponent.vue

    
    
    
    

    One of the main benefits of declaring a component using the Composition API is that it makes logic reuse and extraction very easy. Composition functions are the most straightforward and cost-free way of extending a component by making its features modular and reusable.

提交回复
热议问题