事件js公司
import Vue from 'vue';
export default new Vue({
methods: {
notifySuccess(message) {
this.$emit('notify', 'success', message);
},
notifyInfo(message) {
this.$emit('notify', 'info', message);
},
notifyWarning(message) {
this.$emit('notify', 'warning', message);
},
notifyDanger(message) {
this.$emit('notify', 'danger', message);
},
requestError(error) {
if (error.response) {
this.notifyDanger(error.response.status + ': ' + error.response.statusText);
}
},
},
});
父组件如下所示
<template>
<router-view></router-view>
</template>
<script>
import Event from './js/event.js';
export default {
name: 'app',
created() {
Event.$on('notify', (type, message) => alert(`${type}\n${message}`));
}
}
</script>
标题。vue
<template>
<a class="navbar-brand" href="#" @click.prevent="clickTest"></a>
</template>
<script>
name: 'header',
methods: {
clickTest() {
Event.$emit('notify', 'success', 'Hello World!');
}
}
</script>
我将在未来的弹出窗口中添加例如notifySuccess以绿色显示弹出窗口,notifyDanger以红色显示,notifyWarning以黄色显示。
如何根据nostrification的类型在子组件中创建事件/方法?我做错了什么?
PS:对不起,我的英语不好。我希望你能理解我。