vue component動態組件


下面看下vue component動態組件(詳情看vue.js官網動態組件)

 動態組件

通過component標簽 的is屬性來進行組件的切換

is的屬性值決定要顯示的組件,所以將is的屬性值設置為data中的值,以便於動態變化

1
2
3
4
5
6
7
<template>
   <div class= "app" >
       <component :is= "組件名稱" >
  
       </component>
   </div>
</template>

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
<!DOCTYPE html>
<html lang= "en" >
<head>
   <meta charset= "UTF-8" >
   <title>動態組件</title>
   <script src= "bower_components/vue/dist/vue.js" ></script>
   <style>
   </style>
</head>
<body>
   <div id= "box" >
     <input type= "button"  @click= "a='aaa'"  value= "aaa組件" >
     <input type= "button"  @click= "a='bbb'"  value= "bbb組件" >
     <component :is= "a" ></component> <!-- 動態組件-->
   </div>
 
   <script>
     var  vm= new  Vue({
       el: '#box' ,
       data:{
         a: 'aaa'
       },
       components:{
         'aaa' :{
           template: '<h2>我是aaa組件</h2>'
         },
         'bbb' :{
           template: '<h2>我是bbb組件</h2>'
         }
       }
     });
 
   </script>
</body>
</html>


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM