SoFunction
Updated on 2025-04-04

vue19 instance code for building component, component template, dynamic component

The specific code is as follows:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Document</title>
  <script src="bower_components/vue/dist/"></script>
  <style>
  </style>
</head>
<body>
  <div >
  </div>
  <script>
    var Aaa=({//Inherit a Vue class Aaa      template:'<h3>I am title 3</h3>'
    });
    var a=new Aaa();//a is the same as vm    (a);
    var vm=new Vue({
      el:'#box',
      data:{
        bSign:true
      }
    });
  &lt;/script&gt;
&lt;/body&gt;
&lt;/html&gt;
&lt;!DOCTYPE html&gt;
&lt;html lang="en"&gt;
&lt;head&gt;
  &lt;meta charset="UTF-8"&gt;
  &lt;title&gt;Document&lt;/title&gt;
  &lt;script src="bower_components/vue/dist/"&gt;&lt;/script&gt;
  &lt;style&gt;
  &lt;/style&gt;
&lt;/head&gt;
&lt;body&gt;
  &lt;div &gt;
    &lt;aaa&gt;&lt;/aaa&gt;
  &lt;/div&gt;
  &lt;script&gt;
    var Aaa=({
      template:'<h3>I am title 3</h3>'
    });
    ('aaa',Aaa);//aaa is a component of the component    var vm=new Vue({
      el:'#box',
      data:{
        bSign:true
      }
    });
  &lt;/script&gt;
&lt;/body&gt;
&lt;/html&gt;
&lt;!DOCTYPE html&gt;
&lt;html lang="en"&gt;
&lt;head&gt;
  &lt;meta charset="UTF-8"&gt;
  &lt;title&gt;Document&lt;/title&gt;
  &lt;script src="bower_components/vue/dist/"&gt;&lt;/script&gt;
  &lt;style&gt;
  &lt;/style&gt;
&lt;/head&gt;
&lt;body&gt;
  &lt;div &gt;
    &lt;aaa&gt;&lt;/aaa&gt;
  &lt;/div&gt;

  &lt;script&gt;
    var Aaa=({
      data(){
        return {
          msg:'I'm the title^^'
        };
      },
      template:'&lt;h3&gt;{{msg}}&lt;/h3&gt;'
    });

    ('aaa',Aaa);


    var vm=new Vue({
      el:'#box',
      data:{
        bSign:true
      }
    });

  &lt;/script&gt;
&lt;/body&gt;
&lt;/html&gt;
&lt;!DOCTYPE html&gt;
&lt;html lang="en"&gt;
&lt;head&gt;
  &lt;meta charset="UTF-8"&gt;
  &lt;title&gt;Document&lt;/title&gt;
  &lt;script src="bower_components/vue/dist/"&gt;&lt;/script&gt;
  &lt;style&gt;
  &lt;/style&gt;
&lt;/head&gt;
&lt;body&gt;
  &lt;div &gt;
    &lt;aaa&gt;&lt;/aaa&gt;
  &lt;/div&gt;

  &lt;script&gt;
    var Aaa=({
      data(){
        return {
          msg:'I'm the title^^'
        };
      },
      methods:{
        change(){
          ='changed'
        }
      },
      template:'&lt;h3 @click="change"&gt;{{msg}}&lt;/h3&gt;'
    });

    ('aaa',Aaa);


    var vm=new Vue({
      el:'#box',
      data:{
        bSign:true
      }
    });

  &lt;/script&gt;
&lt;/body&gt;
&lt;/html&gt;


&lt;!DOCTYPE html&gt;
&lt;html lang="en"&gt;
&lt;head&gt;
  &lt;meta charset="UTF-8"&gt;
  &lt;title&gt;Document&lt;/title&gt;
  &lt;script src="bower_components/vue/dist/"&gt;&lt;/script&gt;
  &lt;style&gt;
  &lt;/style&gt;
&lt;/head&gt;
&lt;body&gt;
  &lt;div &gt;
    &lt;aaa&gt;&lt;/aaa&gt;
  &lt;/div&gt;

  &lt;script&gt;
    var Aaa=({
      template:'&lt;h3&gt;{{msg}}&lt;/h3&gt;',
      data(){// es6 syntax, function does not write:, put data in the component: data must be in the form of a function, function must return an object (json)        return {
          msg:'ddddd'
        }
      }
    });


    var vm=new Vue({
      el:'#box',
      data:{
        bSign:true
      },
      components:{ //Part component, put inside a component, ('aaa',Aaa);        aaa:Aaa
      }
    });

  &lt;/script&gt;
&lt;/body&gt;
&lt;/html&gt;
&lt;!DOCTYPE html&gt;
&lt;html lang="en"&gt;
&lt;head&gt;
  &lt;meta charset="UTF-8"&gt;
  &lt;title&gt;Document&lt;/title&gt;
  &lt;script src="bower_components/vue/dist/"&gt;&lt;/script&gt;
  &lt;style&gt;
  &lt;/style&gt;
&lt;/head&gt;
&lt;body&gt;
  &lt;div &gt;
    &lt;my-aaa&gt;&lt;/my-aaa&gt;
  &lt;/div&gt;
  &lt;script&gt;
    var Aaa=({
      template:'&lt;h3&gt;{{msg}}&lt;/h3&gt;',
      data(){
        return {
          msg:'ddddd'
        }
      }
    });
    var vm=new Vue({
      el:'#box',
      data:{
        bSign:true
      },
      components:{ //Local components        'my-aaa':Aaa
      }
    });
  &lt;/script&gt;
&lt;/body&gt;
&lt;/html&gt;
&lt;!DOCTYPE html&gt;
&lt;html lang="en"&gt;
&lt;head&gt;
  &lt;meta charset="UTF-8"&gt;
  &lt;title&gt;Document&lt;/title&gt;
  &lt;script src="bower_components/vue/dist/"&gt;&lt;/script&gt;
  &lt;style&gt;
  &lt;/style&gt;
&lt;/head&gt;
&lt;body&gt;
  &lt;div &gt;
    &lt;my-aaa&gt;&lt;/my-aaa&gt;
  &lt;/div&gt;
  &lt;script&gt;
    ('my-aaa',{//Global, public proposal      template:'<strong>good</strong>'
    });
    var vm=new Vue({
      el:'#box'
    });
  &lt;/script&gt;
&lt;/body&gt;
&lt;/html&gt;
&lt;!DOCTYPE html&gt;
&lt;html lang="en"&gt;
&lt;head&gt;
  &lt;meta charset="UTF-8"&gt;
  &lt;title&gt;Document&lt;/title&gt;
  &lt;script src="bower_components/vue/dist/"&gt;&lt;/script&gt;
  &lt;style&gt;
  &lt;/style&gt;
&lt;/head&gt;
&lt;body&gt;
  &lt;div &gt;
    &lt;my-aaa&gt;&lt;/my-aaa&gt;
  &lt;/div&gt;
  &lt;script&gt;
    var vm=new Vue({
      el:'#box',
      components:{ //Parent        'my-aaa':{
          data(){
            return {
              msg:'welcome vue'
            }
          },
          methods:{
            change(){
              ='changed';
            }
          },
          template:'&lt;h2 @click="change"&gt;title2-&gt;{{msg}}&lt;/h2&gt;'
        }
      }
    });
  &lt;/script&gt;
&lt;/body&gt;
&lt;/html&gt;
&lt;!DOCTYPE html&gt;
&lt;html lang="en"&gt;
&lt;head&gt;
  &lt;meta charset="UTF-8"&gt;
  &lt;title&gt;Document&lt;/title&gt;
  &lt;script src="bower_components/vue/dist/"&gt;&lt;/script&gt;
  &lt;style&gt;
  &lt;/style&gt;
&lt;/head&gt;
&lt;body&gt;
  &lt;div &gt;
    &lt;my-aaa&gt;&lt;/my-aaa&gt;
  &lt;/div&gt;

  &lt;template &gt;
    &lt;h1&gt;title1&lt;/h1&gt;
    &lt;ul&gt;
      &lt;li v-for="val in arr"&gt;
        {{val}}
      &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/template&gt;

  &lt;script&gt;
    var vm=new Vue({
      el:'#box',
      components:{
        'my-aaa':{
          data(){
            return {
              msg:'welcome vue',
              arr:['apple','banana','orange']
            }
          },
          methods:{
            change(){
              ='changed';
            }
          },
          template:'#aaa'
        }
      }
    });

  &lt;/script&gt;
&lt;/body&gt;
&lt;/html&gt;
&lt;!DOCTYPE html&gt;
&lt;html lang="en"&gt;
&lt;head&gt;
  &lt;meta charset="UTF-8"&gt;
  &lt;title&gt;Document&lt;/title&gt;
  &lt;script src="bower_components/vue/dist/"&gt;&lt;/script&gt;
  &lt;style&gt;
  &lt;/style&gt;
&lt;/head&gt;
&lt;body&gt;
  &lt;div &gt;
    &lt;my-aaa&gt;&lt;/my-aaa&gt;
  &lt;/div&gt;

  &lt;script type="x-template" &gt;
    &lt;h2 @click="change"&gt;title2-&gt;{{msg}}&lt;/h2&gt;
    &lt;ul&gt;
      &lt;li&gt;1111&lt;/li&gt;
      &lt;li&gt;222&lt;/li&gt;
      &lt;li&gt;3333&lt;/li&gt;
      &lt;li&gt;1111&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/script&gt;

  &lt;script&gt;
    var vm=new Vue({
      el:'#box',
      components:{
        'my-aaa':{
          data(){
            return {
              msg:'welcome vue'
            }
          },
          methods:{
            change(){
              ='changed';
            }
          },
          template:'#aaa'
        }
      }
    });

  &lt;/script&gt;
&lt;/body&gt;
&lt;/html&gt;
&lt;!DOCTYPE html&gt;
&lt;html lang="en"&gt;
&lt;head&gt;
  &lt;meta charset="UTF-8"&gt;
  &lt;title&gt;Dynamic Components&lt;/title&gt;
  &lt;script src="bower_components/vue/dist/"&gt;&lt;/script&gt;
  &lt;style&gt;
  &lt;/style&gt;
&lt;/head&gt;
&lt;body&gt;
  &lt;div &gt;
    &lt;input type="button" @click="a='aaa'" value="aaa component"&gt;
    &lt;input type="button" @click="a='bbb'" value="bbb component"&gt;
    &lt;component :is="a"&gt;&lt;/component&gt; &lt;!-- Dynamic Components--&gt;
  &lt;/div&gt;

  &lt;script&gt;
    var vm=new Vue({
      el:'#box',
      data:{
        a:'aaa'
      },
      components:{
        'aaa':{
          template:'<h2>I am aaa component</h2>'
        },
        'bbb':{
          template:'<h2>I am a bbb component</h2>'
        }
      }
    });

  &lt;/script&gt;
&lt;/body&gt;
&lt;/html&gt;

Let's take a look at the dynamic component of vue component

Dynamic Components

Component switching is through the is attribute of the component tag

The property value of is determines the component to be displayed, so set the property value of is to the value in data to facilitate dynamic changes.

&lt;template&gt;
  &lt;div class="app"&gt;
      &lt;component :is="Component Name"&gt;
 
      &lt;/component&gt;
  &lt;/div&gt;
&lt;/template&gt;

Summarize

The above is the example code for vue19 to form component, component templates, and dynamic components introduced to you. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support for my website!