zoukankan      html  css  js  c++  java
  • Vue入门二:组件与实例

    vue入门学习笔记,vue.js下载
     
     目录:
    1. 组件
    2. 组件与实例的关系
    3. 父子组件通信:发布订阅模式
    • 组件

    组件指页面中的一部分

    <!DOCTYPE html>
    <html lang="en">
      <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width,initial-scale=1.0">
        <title>Vue Learning</title>
        <script src="./vue.js"></script
      </head>
      <body>
        <div id="app">
            <div>
                <input v-model="inputValue"/>
                <button @click="handleClick">提交</button>
            </div>
            <ul>
                <todo-item v-for="(item,index) of itemValues" 
                            :key="index" 
                            :content="item"> <!-- 定义属性content来传递值-->        
                </todo-item>
            </ul>
        </div>
        
        <script>
            <!-- 全局组件 -->
            Vue.component("todo-item",{
                props:["content"],//子组件中定义props来接收父组件传递来的参数,然后才能在模板中使用
                template:"<li>{{content}}</li>"
            })
            <!-- 局部组件 使用前需要在Vue实例中使用components声明 -->
            //var TodoItem={
            //    template:"<li>item</li>"
            //}
            
            new Vue({
                el:"#app",
                //components:{
                //    "todo-item":TodoItem
                //},
                data:{
                    inputValue:"",
                    itemValues:[]
                },
                methods:{
                    handleClick:function(){
                        this.itemValues.push(this.inputValue)
                        this.inputValue=""
                    }
                }
            })
        </script>
      </body>
    </html>
    • 组件与实例的关系

    每一个组件也是一个vue的实例,实例拥有的属性和方法组件也有。父组件不定义模板是因为它把子组件当做模板。

    <!DOCTYPE html>
    <html lang="en">
      <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width,initial-scale=1.0">
        <title>Vue Learning</title>
        <script src="./vue.js"></script
      </head>
      <body>
        <div id="app">
            <div>
                <input v-model="inputValue"/>
                <button @click="handleSubmit">提交</button>
            </div>
            <ul>
                <todo-item v-for="(item,index) of itemValues" 
                            :key="index" 
                            :content="item">     
                </todo-item>
            </ul>
        </div>
        
        <script>
            Vue.component("todo-item",{
                props:["content"],
                template:"<li @click='handleClick'>{{content}}</li>",//子组件也拥有属性
                methods:{//子组件也拥有方法
                    handleClick:function(){
                        alert("123")
                    }
                }
            })
    
            new Vue({
                el:"#app",
                data:{
                    inputValue:"",
                    itemValues:[]
                },
                methods:{
                    handleSubmit:function(){
                        this.itemValues.push(this.inputValue)
                        this.inputValue=""
                    }
                }
            })
        </script>
      </body>
    </html>
    • 实现todolist的删除功能

    子组件与父组件通信才能实现子组件对父组件数据的操作,通过发布订阅来完成。

    <!DOCTYPE html>
    <html lang="en">
      <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width,initial-scale=1.0">
        <title>Vue Learning</title>
        <script src="./vue.js"></script
      </head>
      <body>
        <div id="app">
            <div>
                <input v-model="inputValue"/>
                <button @click="handleSubmit">提交</button>
            </div>
            <ul>
                <todo-item v-for="(item,index) of itemValues" 
                            :key="index" 
                            :content="item"
                            :index="index"
                            @delete="handleDelete"><!--2 父组件订阅了事件delete-->     
                </todo-item>
            </ul>
        </div>
        
        <script>
            Vue.component("todo-item",{
                props:["content","index"],
                template:"<li @click='handleClick'>{{content}}</li>",
                methods:{
                    handleClick:function(){
                        this.$emit('delete',this.index)<!--1 子组件发布一个事件delete--> 
                    }
                }
            })
    
            new Vue({
                el:"#app",
                data:{
                    inputValue:"",
                    itemValues:[]
                },
                methods:{
                    handleSubmit:function(){
                        this.itemValues.push(this.inputValue)
                        this.inputValue=""
                    },
                    handleDelete:function(index){<!--3 父组件处理handleDelete-->
                        this.itemValues.splice(index,1)
                    }
                }
            })
        </script>
      </body>
    </html>
  • 相关阅读:
    二元查找树的后序遍历结果
    CFileDialog设置多选时的一个问题
    KanRSS.com
    由shuttle这个单词想起的一个小故事
    Sun Java moved to the Partner repository
    Sun Java moved to the Partner repository
    扩展std::string功能的几个做法
    NetBeans 时事通讯(刊号 # 99 Apr 16, 2010)
    NetBeans IDE 6.9 Beta 发布
    KanRSS.com
  • 原文地址:https://www.cnblogs.com/hoaprox/p/14159331.html
Copyright © 2011-2022 走看看