Vue起步

第二章 Vue起步

一、 课程学习方法

Vue官网:https://cn.vuejs.org/

二、 hello world

1. 输出Hello World

(1). 原生JS

<!DOCTYPE html>
<html lang = "en">
<head>
    <meta charset="utf-8" />
    <title>Hello World</title>
</head>
<body>
    <div id="app"></div>
    <script>
        var dom = document.getElementById('app');
        dom.innerHTML = "hello world"
    </script>
</body>
</html>

(2). Vue

<!DOCTYPE html>
<html lang = "en">
<head>
    <meta charset="utf-8" />
    <title>Hello World</title>
  <!--引入vue-->
    <script src="./vue.js"></script>
</head>
<body>
  <!--通过{{}}使用dada中的数据-->
    <div id="app">{{content}}</div>
    <script>
        var app = new Vue({
      //挂载点,只处理此挂载点下的dom内容
            el: '#app',
      //定义一些数据
            data: {
                content: 'hello world'
            }
        })
    </script>
</body>
</html>

2. 定时修改内容

(1). 原生JS

var dom = document.getElementById('app');
dom.innerHTML = "hello world"
setTimeout(function() {
  dom.innerHTML = "bye world"
}, 2000)

(2). Vue.js

var app = new Vue({
    el: '#app',
    data: {
        content: 'hello world'
    }
})
setTimeout(function() {
    app.$data.content = 'bye world'
}, 2000)

三、开发TodoList(v-model、v-for、v-on)

1. 一个网址

www.todolist.cn

2.我的TodoList

<!DOCTYPE html>
<html lang = "en">
<head>
    <meta charset="utf-8" />
    <title>TodoList</title>
    <script src="./vue.js"></script>
</head>
<body>
    <div id="app">
        <!--v-model: 双向显示-->
        <input type="text" v-model="inputValue"/>
        <!--v-on: 绑定事件-->
        <button v-on:click="handleBtnClick">提交</button>
        <ul>
            <!--v-for: 循环显示-->
            <li v-for="item in list">{{item}}</li>
        </ul>
    </div>
    <script>
        var app = new Vue({
            el: '#app',
            data: {
                list: [],
                inputValue: ''
            },
            methods: {
                handleBtnClick: function() {
                    this.list.push(this.inputValue);
                    this.inputValue="";
                }
            }
        })
    </script>
</body>
</html>

四、MVVM模式

五、前端组件化

组件就是页面的一部分

方便维护

六、使用组件改造TodoList

<!DOCTYPE html>
<html lang = "en">
<head>
    <meta charset="utf-8" />
    <title>TodoList</title>
    <script src="./vue.js"></script>
</head>
<body>
    <div id="app">
        <!--v-model: 双向显示-->
        <input type="text" v-model="inputValue"/>
        <!--v-on: 绑定事件-->
        <button v-on:click="handleBtnClick">提交</button>
        <ul>
            <!--v-bind:属性绑定-->
            <todo-item2 v-bind:content="item" v-for="item in list"></todo-item2>
        </ul>
    </div>
    <script>
        // 注册一个全局组件,可直接使用
        Vue.component("TodoItem", {
            props: ['content'],
            template: "<li>{{content}}</li>"
        })
        // 注册一个局部组件
        var TodoItem2 = {
            props: ['content'],
            template: "<li>{{content}}</li>"
        }

        var app = new Vue({
            el: '#app',
            // 通过components使用局部组件
            components: {
                TodoItem2: TodoItem2
            },
            data: {
                list: [],
                inputValue: ''
            },
            methods: {
                handleBtnClick: function() {
                    this.list.push(this.inputValue);
                    this.inputValue="";
                }
            }
        })
    </script>
</body>
</html>

七、简单的组件间传值

<!DOCTYPE html>
<html lang = "en">
<head>
    <meta charset="utf-8" />
    <title>TodoList</title>
    <script src="./vue.js"></script>
</head>
<body>
    <div id="app">
        <!--v-model: 双向绑定-->
        <input type="text" v-model="inputValue"/>
        <!--v-on: 绑定事件-->
        <button v-on:click="handleBtnClick">提交</button>
        <ul>
            <!--v-bind:属性绑定,:为简写-->
            <todo-item v-bind:content="item"
                       :index="index"
                       v-for="(item, index) in list"
                       @delete="handleItemDelete">
            </todo-item>
        </ul>
    </div>
    <script>
        // 注册一个全局组件,可直接使用
        Vue.component("TodoItem2", {
            props: ['content'],
            template: "<li>{{content}}</li>"
        })
        // 注册一个局部组件
        var TodoItem = {
            props: ['content', 'index'],
            //@是v-on的简写
            template: "<li @click='handleItemClick'>{{content}}</li>",
            methods: {
                handleItemClick: function() {
                    //提交一个事件
                    this.$emit('delete',this.index);
                }
            }
        }

        var app = new Vue({
            el: '#app',
            // 通过components使用局部组件
            components: {
                TodoItem: TodoItem
            },
            data: {
                list: [],
                inputValue: ''
            },
            methods: {
                handleBtnClick: function() {
                    this.list.push(this.inputValue);
                    this.inputValue="";
                },
                handleItemDelete: function(index) {
                    this.list.splice(index, 1);
                }
            }
        })
    </script>
</body>
</html>
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 潘雨馨,11月5日,读书打卡第23次,今天我读了三国演义第329-340页,本章讲述的主要人物是吕蒙,吕蒙是三国时...
    潘雨馨阅读 228评论 0 0
  • 清晨,玲轻轻的推开窗,眼前是两朵含苞欲放的红玫瑰。那颗晶莹的露珠,从花蕾上滑落下来,那绵绵的细雨,薄得像一层轻...
    冉雪颜阅读 340评论 0 0
  • 2017年,想必是我25岁本命年,糟糕的一年吧。 我今年25岁,大学刚毕业,PS:上学上的晚。一直走在单身的这条路...
    三碗带尖儿阅读 1,063评论 24 4

友情链接更多精彩内容