简体   繁体   English

为什么我的 watch 方法在 Vue 中不起作用

[英]Why is my watch method didn't work in Vue

I have two components in one js file, file starts with const EventBus = new Vue();我在一个js文件中有两个组件,文件以const EventBus = new Vue();开头const EventBus = new Vue();

I want to send two strings 'username' and 'password' from first component to seconod component, but I can't do it.我想将两个字符串“用户名”和“密码”从第一个组件发送到第二个组件,但我做不到。 Where I am wrong?我错在哪里?

First:第一的:

Vue.component('log-reg-modal', {
    data: () => ({
        username: "",
        password: "",
    }),
    watch: {
        username: function(){
            EventBus.$emit('changed_username', this.username);
        },
        password: function(){
            EventBus.$emit('changed_password', this.password);
        },
    }

});

Second:第二:

new Vue({
    el: '#app',
    vuetify: new Vuetify(),
    data: () => ({
        username: "",
        password: "",
    }),
    methods: {
        register : function (event) {
            EventBus.$on('changed_username', this.username);
            EventBus.$on('changed_password', this.password);
        }
    }
});

You will need to use EventBus.$on within the created lifecycle method, to "register" receiving the events.您需要在created生命周期方法中使用EventBus.$on来“注册”接收事件。

You can also skip the EventBus and just use 'built in' event handlers..您也可以跳过EventBus并仅使用“内置”事件处理程序。

Using Event Bus:使用事件总线:

 const EventBus = new Vue(); const LogRegModal = { template: ` <div><input v-model="username" placeholder="Username" /> <input v-model="password" placeholder="Password" /></div> `, data: () => ({ username: "", password: "" }), watch: { username: function() { EventBus.$emit("changed_username", this.username) }, password: function() { EventBus.$emit("changed_password", this.password) }, } }; new Vue({ el: "#app", components: { LogRegModal }, data: () => ({ username: "", password: "" }), created() { EventBus.$on("changed_username", val => this.username = val); EventBus.$on("changed_password", val => this.password = val); } });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.6.10/vue.min.js"></script> <div id="app"> <Log-Reg-Modal></Log-Reg-Modal> <p v-if="username || password"><i>These values were received from the EventBus:</i></p> <ul v-if="username || password"> <li v-if="username"><b>Username:</b> {{username}}</li> <li v-if="password"><b>Password:</b> {{password}}</li> </ul> </div>

Using 'built-in' this.$emit(..., ...) :使用“内置” this.$emit(..., ...)

 const LogRegModal = { template: ` <div><input v-model="username" placeholder="Username" /> <input v-model="password" placeholder="Password" /></div> `, data: () => ({ username: "", password: "" }), watch: { username: function() { this.$emit("changed_username", this.username) }, password: function() { this.$emit("changed_password", this.password) }, } }; new Vue({ el: "#app", components: { LogRegModal }, data: () => ({ username: "", password: "" }), methods: { handleUsernameChange(val) { this.username = val }, handlePasswordChange(val) { this.password = val } }, });
 code { color: red; } p { margin-bottom: 2px; } .second-p { margin-top: 0px; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.6.10/vue.min.js"></script> <div id="app"> <!-- ******************************************** --> <!-- Notice we are now handling events as props --> <!-- ******************************************** --> <Log-Reg-Modal @changed_username="handleUsernameChange" @changed_password="handlePasswordChange" ></Log-Reg-Modal> <div v-if="username || password"> <p><i>These values were received via </i><code>this.$emit</code> as 'props' on our component using <code>v-on:event_name</code></p> <p class="second-p">(using <code>@event_name</code> is shorthand for <code>v-on:event_name</code>):</p> <ul> <li v-if="username"><b>Username:</b> {{username}}</li> <li v-if="password"><b>Password:</b> {{password}}</li> </ul> </div> </div>

  1. Handlers that are indicated in EventBus.$on should be functions and not props themself. EventBus.$on 中指示的处理程序应该是函数而不是 props 本身。
  2. Did you call register method before starting the use of the log-reg-modal component?在开始使用log-reg-modal组件之前,您是否调用了register方法?

EventBus.$on methods should be in created() functions in according to Vue lifecycle根据 Vue 生命周期, EventBus.$on方法应该在created()函数中

created (){
        EventBus.$on('changed_username', (data) => {
            this.username = data;
          });
        EventBus.$on('changed_password', (data) => {
            this.password = data;
          });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM