Easy to understand - Quick Start Vue - 1

  • Original design patterns: MVP

    M 模型   数据
    V 视图  触发事件
    P 控制器  负责业务逻辑
  • New Design Patterns: MVVM

    M层        script 主要M层开发   面向数据编程
    V层        html
    VM层       进行逻辑(自动改变)
  • VM layer? ? ? ? ? ?
    • Virtual dom ???
    • define proxy mode? ? ?

Components of the project

  • Each component is the area of ​​the page.

1. Global Components

    Vue.component('todo-item',{
        props:['content'],
        template:"<li>{{content}}</li>"
    });

2. The sub-assembly

var TodoItem = {
      props:['content'],
      template:"<li>{{content}}</li>"
    };
  • Local assemblies need to register
// 在实例中进行注册
var vm = new Vue({
    el:"#app",
    // 局部组件注册
        components:{
            TodoItem:TodoItem
        },
})

3 pass values ​​to the parent component subassembly

  • Parent component by v-bind: xxx = "xxx" to bind the values ​​of mass, is defined by the sub-assembly by The props, which parent component value came to accept,

    v-bind:子组件props接收要穿值的名字=父组件变量的名字

4 sub-assembly to the parent component value transfer

  • Event subassembly by $ emit, that will trigger the spread parent component, the parent component by listening to the event, to change the value of
// 子组件绑定要触发事件 @click='handelItemClick'
handelItemClick:function () {
                this.$emit('delete',this.index)
          }
//通过$emit触发事件,并传值。


//父组件在标签上监听该delete事件,并绑定处理方法
@delete="handelItemDelete"

//父组件定义handelItemDelete,将传来的数据,在当前实例中修改
handelItemDelete:function (indexvalue) {
            this.list.splice(indexvalue,1)
}
  • v-bind: equal to: v-on: is equal to:

5 a component Todolist

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<script src="./vue.js"></script>
<body>
<div id="app">
    <input type="text" v-model="inputValue">
    <button @click="handlebtnClisk">提交</button>
    <ul>
        <todo-item v-for="(item,index) in list"
                   v-bind:index="index"
                   v-bind:content="item"
                   @delete="handelItemDelete"

        >{{item}}</todo-item>
    </ul>
</div>
</body>
<script>
    // 全局组件
    // Vue.component('todo-item',{
    //     props:['content'],
    //     template:"<li>{{content}}</li>"
    // });
    // 局部组件
    var TodoItem = {
      props:['content','index'],
      template:"<li @click='handelItemClick'>{{content}}</li>",
      methods:{
          handelItemClick:function () {
                this.$emit('delete',this.index)
          }
      }
    };




    var vm = new Vue({
        // el限制一个vue实例的管理范围。
        el:"#app",
        // 局部组件注册
        components:{
            TodoItem:TodoItem
        },
        data:{
            list:[],
            inputValue:""
        },
        methods:{
            handlebtnClisk:function () {
                this.list.push(this.inputValue);
                this.inputValue = ""
            },
            handelItemDelete:function (indexvalue) {
                this.list.splice(indexvalue,1)
            }
        }
    });

</script>
</html>

Guess you like

Origin www.cnblogs.com/xujunkai/p/12229974.html