SoFunction
Updated on 2025-04-05

Detailed explanation of component and routing usage example code in Vue

1. What is the component

Component systems are an important concept in Vue because they are an abstraction that allows us to build large applications using small, independent and often reusable components. Usually an application is organized in the form of a nested component tree;

1.1 Declaration and use of components

Global Components

<body>
  <div >
    <!-- Use the global component name asHTMLTags -->
    <myzujian></myzujian>
  </div>
  
</body>
<script>
  //Set global components   ("myzujian",{
     template:"<h2>I am a global component</h2>"
   });
   var app=new Vue({
     el:"#app",
   });
&lt;/script&gt;

Local components

&lt;body&gt;
  &lt;div &gt;
    &lt;!-- Use the name of the local component asHTMLTags --&gt;
    &lt;zu-jian&gt;&lt;/zu-jian&gt;
  &lt;/div&gt;
  
&lt;/body&gt;
&lt;script&gt;
   var app=new Vue({
     el:"#app",
     components:{
       zuJian:{
         template:"<h1>I am a local component</h1>", 
       }
     }
   });
&lt;/script&gt;

1.2 Notes on components

If the component name is named after the camel method, the uppercase letters should be changed to lowercase when using the component, and the - Tamplate attribute in the component must have a unique root element, otherwise an error will be reported.

1.3 Data and methods in components

<body>
  <div >
    <mytemp></mytemp>
  </div>
</body>
<script>
  var app=new Vue({
    el:"#app",
    data:{},
    components:{
      mytemp:{
        data(){
          return {
            msg:"123456789",
          }
        },
        methods: {
          cli(){
            alert();
          }
        },
        template:'<h1 @click="cli">you{{msg}} very good</h1>',
      }
    }
  });
</script>

1.4 Parent component passes value

&lt;body&gt;
  &lt;div &gt;
    &lt;my :cc="msg"&gt;&lt;/my&gt;
  &lt;/div&gt;
&lt;/body&gt;
&lt;script&gt;
  var app = new Vue({
    el:'#app',
    data:{msg:'Nothing to do'},
    components:{
      my:{
        props:['cc'],
        template:"&lt;s&gt;{{cc}}&lt;/s&gt;"
      }
    }
  })
&lt;/script&gt;

2. Use of routing

Vue is using the routing plug-in Vue-router and needs to be introduced in advance.

&lt;body&gt;
  &lt;div &gt;
    &lt;ul&gt;
      &lt;li&gt; &lt;router-link to="/login"&gt;Log in&lt;/router-link&gt; &lt;/li&gt;
      &lt;li&gt; &lt;router-link to="/reg"&gt;register&lt;/router-link&gt; &lt;/li&gt;
    &lt;/ul&gt;
    &lt;router-view&gt;&lt;/router-view&gt;
  &lt;/div&gt;
&lt;/body&gt;
&lt;script&gt;
  // Get the routing object  var ro = new VueRouter({
    // Define routing rules    routes:[
      // Specific matching rules      // {path: match the address bar route changes, component: to display the component}      {path:'/reg',component:{template:"<s>I am registering</s>"}},
      {path:'/login',component:{template:"<s>I am logged in</s>"}},
    ]
  })
  var app = new Vue({
    el: '#app',
    data: {},
    router:ro
  })
&lt;/script&gt;

2.1 Dynamic routing matching

&lt;body&gt;
  &lt;div &gt;
    &lt;!-- Pass the data directly in / later --&gt;
    &lt;router-link to="/user/10"&gt;hfhg&lt;/router-link&gt;
    &lt;router-view&gt;&lt;/router-view&gt;
  &lt;/div&gt;
&lt;/body&gt;
&lt;script&gt;
  var router = new VueRouter({
    routes: [
      {  
        // Get the data as the name of the variable is preceded by:        path: "/user/:id", component: {
          mounted(){
            // router will add public attribute $route to vue, and use $route to get data            (this.$)
          },
          template: "&lt;s&gt;Just a master{{$}}Separate the country&lt;/s&gt;"
        }
      }
    ]
  })
  var app = new Vue({
    el: '#app',
    data: {},
    router,
  })
&lt;/script&gt;

Summarize

The above is a detailed explanation of the components and routing usage example codes in Vue introduced to you by the editor. 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!
If you think this article is helpful to you, please reprint it. Please indicate the source, thank you!