【问题标题】:Vue Js Component issue- manipulating dataVue Js 组件问题 - 处理数据
【发布时间】:2019-11-27 20:06:31
【问题描述】:

我一直在为一个类开发一个项目,但遇到了一个问题,即组件不能按预期很好地相互通信。目标是,如果它发送消息的任何组件中有一个错误,则不要转到该位置。 这是我目前拥有的一些代码:

    <template>
    <div class="component">
        <h3>Temperature Sensor</h3>

        <input v-model = 'temperature' @keyup.enter = "checkTemp"> </input>
        <p> Action to take : {{ action }} </p>

    </div>
</template>

<script>
import { eventBus } from '../main.js';
export default {

    props :{ 
        action : String
    },
    computed () {
        return {
            //defines a movement value that we will use.
            movement : true,
        };
    },
    mounted(){
        eventBus.$on('move',(movement) =>{
           this.movement = movement;
           if(movement == true){
               this.action = "Go to Location";
           }else{
               this.action = "Dont go There";
           }
        })
    },
    methods : {

        checkTemp(){
            if(this.temperature >=50){
                this.movement = false;
                console.log(this.movement);
                eventBus.$emit('move', this.movement);
            }else {
                this.movement = true;
                console.log(this.movement);
                eventBus.$emit('move', this.movement);
            }
        }
    },

};
</script>

<style scoped>
    div {
        background-color: lightcoral;
    }
</style>

对于第二个组件:

    <template>
    <div class="component">
        <h3> Radiation Drone</h3>
       <input v-model = 'radiation' @keyup.enter = "checkRadiation"></input>
        <p > Action : {{ action }} </p>

    </div>
</template>

<script>
import { eventBus } from '../main.js';

export default {
    props : {

        action : String
    },
    computed () {
        return {
            movement : true,
        }
    },
    mounted(){
        eventBus.$on('move',(movement) =>{
           this.movement = movement;
           if(movement == true){
               this.action = "Go to Location";
           }else{
               this.action = "Dont go There";
           }
        })
    },
    methods : { 
      checkRadiation(){

              if(this.radiation >=34){
                  this.movement = false;
                  console.log(this.movement);
                 eventBus.$emit('dontGo',this.movement);
              }
                else {
                  this.movement = true;
                  console.log(this.movement);
                  this.$root.$emit('goThere',this.movement);
              }

          }
      },
};

</script>

<style scoped>
    div {
        background-color: lightgreen;
    }
</style>

显示不直接操作道具的错误我只是不知道如何重构我的代码。 有什么建议吗?

【问题讨论】:

    标签: javascript vue.js components


    【解决方案1】:

    您不能直接从子组件更改 prop 值,而是使用emit 调用在父组件中创建的方法

    【讨论】:

    • 所以我会发出正确的方法,在另一个组件中修改它并在那里使用它或者它是如何工作的?
    • 是的!检查一下我制作的小提琴:jsfiddle.net/hansfelix50/ukrz98x3
    猜你喜欢
    • 1970-01-01
    • 2020-12-07
    • 2018-07-21
    • 2019-08-11
    • 2017-03-15
    • 2017-03-06
    • 2019-03-14
    • 1970-01-01
    • 2017-12-27
    相关资源
    最近更新 更多