Home  >  Article  >  Web Front-end  >  How to define a component in vue.js

How to define a component in vue.js

下次还敢
下次还敢Original
2024-04-02 00:45:18779browse

There are three ways to define a component in Vue.js: directly define it in the <script> tag and export the component object, create it using the component factory function and defineComponent auxiliary function, and use a class to define the component and inherit Vue.extend to create it. .

How to define a component in vue.js

How to define components in Vue.js

1. Direct definition

  1. In the <script> tag, use export default {} to export the component object:

    <code class="html"><script>
    export default {
      // 组件内容
    }
    </script></code>
  2. In the <template> tag, define the component layout:

    <code class="html"><template>
      <div>组件内容</div>
    </template></code>

2. Use the component factory function

  1. Create a factory function and use defineComponent Auxiliary function:

    <code class="javascript">import { defineComponent } from 'vue'
    
    export default defineComponent({
      // 组件内容
    })</code>
  2. The component layout is the same as the direct definition above.

3. Use classes to define components

  1. InheritanceVue.extend Create a class:

    <code class="javascript">import Vue from 'vue'
    
    export default class MyComponent extends Vue {
      // 组件内容
    }</code>
  2. Registered component:

    <code class="javascript">// 在 Vue 实例中
    this.$options.components.MyComponent = MyComponent</code>

The above is the detailed content of How to define a component in vue.js. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:What framework is vue.js?Next article:What framework is vue.js?