码迷,mamicode.com
首页 > 其他好文 > 详细

Vue学习之不同组件之间的消息传递

时间:2019-01-11 23:17:19      阅读:221      评论:0      收藏:0      [点我收藏+]

标签:定义   put   handle   mode   组件   utf-8   场景   dex   temp   

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>vue</title>
    <script src="vue.js"></script>
</head>
<body>

    <!-- watch监听——只能监听包含在watch中 定义 的变量
        watch: {
            msg: function (){
                //打印日志
                console.log()

            }
        }
        computed能监听在computed中所使用的所有变量
            computed: {
                msg1: function() {
                    return
                }
            }
        
        使用场景介绍,watch(一个变量/常量/数组,异步场景),computed(数据联动)
    
        如何进行拆分
        1、不超过300行
        2、复用

        组件化带来的问题:
        1、组件状态管理(vuex)
        2、多组件的混合使用,多页面,复杂业务(vue-router)
        3、组件间的传参、消息、事件管理(props,emit/on,bus)

        vue代码风格
        1、好习惯,少走坑
        2、写出自己看得懂的代码
        3、写出别人看得懂的代码

        vue-router
        1、<router-link to="/info"></router-link> 去连接到组件
        2、在router.js中定义组件
        import Info from './views/Info.vue';
        {
            path:'/info',
            name:'info',
            component:Info,
        }
        3、自己去定义组件
        <template>
            <div></div>
        </template>
        <script>
        </script>
        <style scoped>
        </style>

        vuex 
        1、单项数据流概念
        2、store.js
        {
            //组件的状态
            state: {

            },
            //改变状态的方法集
            mutations: {

            },
            actions: {

            }
        }
        https://www.imooc.com/video/18564

        vue调式方法,浏览器检查元素进入到console
        1、console.log()
        2、alert('sd')
        3、debugger //程序会运行到这里停止

        #### 开发工作流
        + 需求调研(确定需求)
        + 交互设计、逻辑设计、接口设计
        + 代码实现(1/3的时间)、测试运行(1/10)、线上部署

        git简介
        //创建空的仓库,查看本地/远程分支的
        git status
        //查看分支的情况
        git branch -a
        //创建新分支用于开发,名叫dev
        git checkout -b dev

        //把dev分支合并到master
        //首先切换到master
        git check master
        git merge dev
        //删除本地分支
        git branch -D dev
        //删除远程分支
        git push origin :dev

        //版本回退
        git reset --hard head^

        //查看log
        git reflog
        //回退到制定版本
        git reset --hard reglog的id

        打包部署
        cd 目录
        npm build 自动打包,运行完形成dist文件夹,把此文件夹放到服务器就可以访问了
        

    -->

    <!-- 此为父组件模板 -->
    <div id="root">
        <div>
            <input v-model="inputValue" />
            <button @click="handleSubmit">提交</button>
        </div>
        <ul>
            <todo-item
             v-for="(item, index) of list"
             :key="index"
             :content="item"
             :index="index"
             @delete="handleDelete"
            >
            </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)
                }
            }
        })

        // var TodoItem = {
        //     template: '<li>item</li>'
        // }

        //父组件
        new Vue({
            el:"#root",
            // components:{
            //     'todo-item': TodoItem
            // },
            data:{
                inputValue: 'hello',
                list: []
            },
            methods: {
                handleSubmit: function() {
                    this.list.push(this.inputValue)
                    this.inputValue = ''
                },
                handleDelete: function(index){
                    this.list.splice(index, 1)
                }
            }
        })
    </script>
</body>
</html>

Vue学习之不同组件之间的消息传递

标签:定义   put   handle   mode   组件   utf-8   场景   dex   temp   

原文地址:https://www.cnblogs.com/twodoge/p/10257515.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!