javascript - vue dev工具中的警告

标签 javascript vue.js vuejs2 vue-component dom-events

我有一个项目,在项目中我有 3 个文件:

1 。 index.html

2。主.js

3。 Vue.js(Vue 库)

我的 index.html:

    <!DOCTYPE html>
<html>
<head>
    <title></title>
    <style type="text/css">
        body{
            padding-top: 40px;
        }
    </style>
</head>
<body>
    <div id="root" class="container">
        <coupon @applied="OnCouponApplied"></coupon>
        <h1 v-show="couponApplied">applied</h1>
    </div>
    <script src="../index/vue.js"></script>
    <script src="main.js"></script>
</body>
</html>

还有我的 main.js :

    window.Event=new Vue();

Vue.component('coupon',{
    name:'coupon',
    template:'<input placeholder="enter you`r code" @blur="OnCouponApplied">',
    methods:{
        OnCouponApplied(){
            Event.$emit('applied');
        }
    }
});
new Vue({
    el:'#root',
    data:{
        couponApplied:false
    },
    created(){
        Event.$on('applied',()=>alert('handle!'));
    }
});

但我有两个警告:

警告:

vue.js:597 [Vue warn]: Property or method "OnCouponApplied" is not defined on the instance but referenced during render. Make sure that this property is reactive, either in the data option, or for class-based components, by initializing the property. See: https://v2.vuejs.org/v2/guide/reactivity.html#Declaring-Reactive-Properties.

(found in <Root>)

vue.js:597 [Vue warn]: Invalid handler for event "applied": got undefined

found in

---> <Coupon>
<Root>

它工作正常,现在我也想知道是否有解决这些警告的办法?

最佳答案

main.js 中,我们在 Vue 实例中没有 OnCouponApplied

所以我删除了 index.html 中的 OnCouponApplied

并修复 main.js 以获得更好的 View (不要使用 $emit 和 $on):

window.Event=new class{
    constructor(){
        this.vue=new Vue();
    }
    fire(event,data=null){
        this.vue.$emit(event,data);
    }
    listen(event,callback){
        this.vue.$on(event,callback);
    }
};

Vue.component('coupon',{
    name:'coupon',
    template:'<input placeholder="enter you`r code" @blur="OnCouponApplied">',
    methods:{
        OnCouponApplied(){
            Event.fire('applied');
        }
    }
});
new Vue({
    el:'#root',
    created(){
        Event.listen('applied',()=>alert('handle!'));
    }
});

关于javascript - vue dev工具中的警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50626686/

相关文章:

javascript - @ 之前正好 10 个数字的电子邮件地址的正则表达式验证器

javascript - 如何在vuex的actions.js中使用router?

php - 如何在laravel中的同一 View 文件中获取url中参数的值

vue.js - Visual Studio代码: Initial VueJS setup cannot find module "@/.."

javascript - Vue js v-bind 功能不起作用?

javascript - Puppeteer:有没有办法访问 DevTools Network API?

javascript - 在 Canvas 上循环绘制图像不起作用

javascript - 在 Vue.js 中使用 Flow.js

javascript - Vue.js 组件中未定义 props

javascript - 在 Axios 请求中使用 Promise