Home >Web Front-end >Vue.js >A brief analysis of several ways to create components in Vue

A brief analysis of several ways to create components in Vue

醉折花枝作酒筹
醉折花枝作酒筹Original
2021-03-31 11:09:402578browse

Creating vue components is to split the code amount of vue instances and find different functions through different components. Here are three ways to create components in Vue. Friends in need can refer to them. I hope it will be helpful to everyone. A brief analysis of several ways to create components in Vue

Method 1: Use Vue.extend to create a global Vue component

Instance:

var com1 = Vue.extend({
    template: &#39;<h3>这是使用 Vue.extend 创建的组件</h3>&#39; // 通过 template 属性,指定了组件要展示的HTML结构
     })

Use Vue.component('component name', created component object) When creating a vue component, use the first letter of the name to name it. When referencing the component, change the uppercase to lowercase letters, and use # before the two words. ##- Connection; parameter one: a tag to introduce the component, parameter two: template is the HTML content displayed by the component.

<div id="app">
    <!-- 如果要使用组件,直接,把组件的名称,以 HTML 标签的形式,引入到页面中,即可 -->
    <mycom1></mycom1>
  </div>

  <script>
Vue.component(&#39;mycom1&#39;, Vue.extend({
      template: &#39;<h3>这是使用 Vue.extend 创建的组件</h3>&#39;
    }))
    // 创建 Vue 实例,得到 ViewModel
    var vm = new Vue({
      el: &#39;#app&#39;,
      data: {},
    });
  </script>

The result obtained is:

A brief analysis of several ways to create components in Vue

Method 2: Use Vue.component directly

<body>
  <div id="app">
    <!-- 使用标签形式,引入自己的组件 -->
    <mycom2></mycom2>
  </div>

  <script>
    Vue.component(&#39;mycom2&#39;, {
      template:`<div>
                  <h3>这是直接使用 Vue.component 创建出来的组件</h3>
                  <span>123</span>
               </div> `
    })
    // 创建 Vue 实例,得到 ViewModel
    var vm = new Vue({
      el: &#39;#app&#39;,
      data: {},
      methods: {}
    });
  </script>
</body>

Result:

A brief analysis of several ways to create components in Vue

Method 3: Use Vue.component outside the controlled #app, and use the template element

inside the controlled #App Outside, use the template element to define the HTML template structure of the component


<body>
  <div id="app2">
     <login></login>
    <mycom3></mycom3>
    <login></login>
  </div>
  <template id="tmpl">
    <div>
      <h1>这是通过 template 元素,在外部定义的组件结构,这个方式,有代码的只能提示和高亮</h1>
      <h4>好用,不错!</h4>
    </div>
  </template>
  <template id="tmpl2">
    <h1>这是私有的 login 组件</h1>
  </template>
  <script>
    Vue.component(&#39;mycom3&#39;, {
      template: &#39;#tmpl&#39;,
    })
    // 创建 Vue 实例,得到 ViewModel
    var vm2 = new Vue({
      el: &#39;#app2&#39;,
      data: {},
      methods: {},
      filters: {},
      directives: {},
      components: { // 定义实例内部私有组件的
        login: {
          template: &#39;#tmpl2&#39;
        }
      },
    })
  </script>
</body>

Output result:

A brief analysis of several ways to create components in Vue

Note: No matter how the component is created, the content pointed to by the component's template has one and only one root element.


Related recommendations: "

vue.js Tutorial"

The above is the detailed content of A brief analysis of several ways to create components in Vue. 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