zoukankan      html  css  js  c++  java
  • vue兄弟组件之间的传参

    //子传父,父传子
    A1要向A2传值 、 可以用$emit传给A、A在使用v-bind传给A2 
    <one :msg="msg" @decrease="decrease"/>
    <two :msg="msg" @add="add"/>
    可以使用计算属性来转换msg

    使用父组件做中转

    使用eventBus解决问题

    1.创建一个空Vue对象

    import Vue from 'vue';
    export default new Vue;

    2.$emit自定义事件

    <input type="text" v-model="todoList">
    <button class="add" @click='addList'>增加</button>
        data(){
            return {
                todoList:''
            }
        },
        methods:{
            addList:function(){
                eventBus.$emit('add',this.todoList)
            }
        }
    3.$on接收事件

        created:function(){
            this.acceptList()
        },
        methods:{
            acceptList:function(){
                eventBus.$on('add',(message)=>{
                    this.lists.push({ name:message,isFinish:false })
                })
            }
        }
    使用vuex解决问题
    创建store对象
    import Vue from 'vue';
    import Vuex from 'vuex';

    Vue.use(Vuex);

    var store=new Vuex.Store({
        //存储状态
        state:{
            lists:[
                { name:'任务1',isFinish:false },
              ...
            ]
        },
        //显示的更改state
        mutation:{},
        //过滤state中的数据
        getters:{},
        //异步操作
        actions:{}
    });

    export default store;
    在组建中引入并使用
    在组件1中
    <input type="text" v-model="todoList">
    <button class="add" @click='addList'>增加</button>
        data(){
            return {
                todoList:''
            }
        },
        store:store,
        methods:{
            addList:function(){
                this.$store.state.lists.push({name:this.todoList,isFinish:false})
            }
        }
    在组件2中
        computed:{
            lists:function(){
                return this.$store.state.lists
            }
        },
  • 相关阅读:
    安装Eclipse for MAC 苹果版
    visual studio code emmet 插件不提示?解决方案
    支付宝付款页面调整屏幕亮度
    浅谈iOS需要掌握的技术点
    Objective-C 编程艺术 (Zen and the Art of the Objective-C Craftsmanship 中文翻译)
    ios开发数据库版本迁移手动更新迭代和自动更新迭代艺术(-)
    ios 添加到cell 上的button点击无效!扩大button的点击区域(黑魔法)
    个人中心模块-拍照剪裁上传
    利用js与java交互
    显示gif动画(帧动画的播放)
  • 原文地址:https://www.cnblogs.com/7c89/p/14180964.html
Copyright © 2011-2022 走看看