e 7 months ago
parent
commit
8650468f42
2 changed files with 105 additions and 0 deletions
  1. 61 0
      vue/vue初阶/18.组件.html
  2. 44 0
      vue/vue初阶/19.嵌套组件.html

+ 61 - 0
vue/vue初阶/18.组件.html

@@ -0,0 +1,61 @@
+<!DOCTYPE html>
+<html lang="en">
+<head>
+    <meta charset="UTF-8">
+    <meta name="viewport" content="width=device-width, initial-scale=1.0">
+    <title>Document</title>
+</head>
+<body>
+    <div id="app">
+        <!-- <Hello/>
+        <my-com/> -->
+        <vase/>
+    </div>
+    <script src="./vue.js"></script>
+    <!-- 
+    组件:
+        全局组件:Vue.component
+            Vue.component('组件名字',{组件模板})
+            直接使用组件名字即可
+        局部组件:Vue.extend
+            const xxx = Vue.extend({组件模板})
+            在components:{
+                aaa:xxx
+                xxx:xxx => xxx 同词省略 自然渲染的就是xxx
+            }
+            页面渲染的是aaa
+        全局组件直接使用
+        局部组件需要在components里注册后使用
+    -->
+    <script>
+        // Vue.component('组件名字',{组件模板})
+        Vue.component("Hello",{
+            template: `<h1>你好</h1>`
+        })
+      const blue =  Vue.extend({
+            template: `<div>这是Vase</div>`
+        })
+        // Vue.component("my-com",{
+        //     template: `
+        //         <ul>
+        //             <li>1</li>  
+        //             <li>2</li>  
+        //             <li>3</li>  
+        //             <li>4</li>    
+        //         </ul>
+        //     `
+        // })
+        var app = new Vue({
+            data:{},
+            methods:{},
+            computed:{},
+            watch:{},
+            components:{
+                // 注册名字:注册组件
+                vase:blue
+            }
+        }).$mount("#app");
+    </script>
+
+</body>
+</html>

+ 44 - 0
vue/vue初阶/19.嵌套组件.html

@@ -0,0 +1,44 @@
+<!DOCTYPE html>
+<html lang="en">
+  <head>
+    <meta charset="UTF-8" />
+    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
+    <title>Document</title>
+  </head>
+  <body>
+    <div id="app">
+      <!-- <list1/> -->
+      <list2 />
+    </div>
+    <script src="./vue.js"></script>
+    <script>
+      const list1 = Vue.extend({
+        template: `
+                <div>你好, {{name}}</div>
+            `,
+            data(){
+                return {
+                    name: 'John',
+                }
+            }
+      });
+      const list2 = Vue.extend({
+        template: `
+                <div>
+                    <list1/>
+                    我有一个帽衫
+                </div>
+            `,
+        components: {
+          list1,
+        },
+      });
+
+      var app = new Vue({
+        components: {
+          list2,
+        },
+      }).$mount("#app");
+    </script>
+  </body>
+</html>