vue.js - 我无法让我的 vue 组件发出工作
问题描述
我是 vue 的新手(不到一周),我正在重写一个爱好项目以起床。决定试用组件并遇到问题。我的一个子组件发射得很好,但另一个子组件没有被父级接收。vue-devtools chrome 扩展告诉我 sidenavselect 确实发出了 close 事件,但 sidenav v-on:close 没有被触发。
<sidenav v-on:close="sidebar_show = false" v-show="sidebar_show">
<sidenavbutton @click="draw_board">Start Round</sidenavbutton>
<sidenavselect v-model="location" :datafield="locations" title="Location"></sidenavselect>
</sidenav>
Vue.component('sidenav', {
props: ['method'],
template: `
<div class="sidenav" v-on:close="handle_close" @click.self="handle_close">
<div class="contents" v-on:close="handle_close">
<span class="closebtn" v-on:click="handle_close">×</span>
<slot></slot>
</div>
</div>
`,
methods: {
handle_close: function() {
this.$emit('close');
}
}
});
Vue.component('sidenavbutton', {
template: `
<button tabindex="-1" @click="handle_click"><slot></slot></button>
`,
methods: {
handle_click: function() {
this.$emit('click');
this.$emit('close');
}
}
});
Vue.component('sidenavselect', {
props: ['datafield', 'title', 'value'],
template: `
<div class="sidenav-box">
{{title}}<br>
<select tabindex="-1" v-bind:value="value" @input="handle_close">
<option v-for="this_data in datafield" :value="this_data.value">{{this_data.label}}</option>
</select>
</div>
`,
methods: {
handle_close: function(event) {
this.$emit('input', event.target.value);
this.$emit('close');
}
}
});
解决方案
Vue.component("sidenav", {
template: `
<div class="sidenav" @close="handle_close" @click.self="handle_close">
<div class="contents" @close="handle_close">
<span class="closebtn" @click="handle_close">×</span>
<slot></slot>
</div>
</div>
`,
methods: {
handle_close: function() {
this.$emit("close");
}
}
});
Vue.component("sidenavbutton", {
template: `
<button @click="handle_click"><slot></slot></button>
`,
methods: {
handle_click: function() {
this.$emit("click");
this.$emit("close");
}
}
});
Vue.component("sidenavselect", {
props: ["value"],
template: `
<div class="sidenav-box">
<select :value="value" @input="handle_close">
<option value="1">A</option>
<option value="2">B</option>
<option value="3">C</option>
</select>
</div>
`,
methods: {
handle_close: function(event) {
this.$emit("input", event.target.value);
this.$emit("close");
}
}
});
new Vue({
data() {
return {
sidebar_show: true,
location: 0
};
},
methods: {
handle_close: function(event) {
this.sidebar_show = false;
}
}
}).$mount("#app");
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
<sidenav @close="handle_close" v-show="sidebar_show">
<sidenavbutton @close="handle_close">Start Round</sidenavbutton>
<sidenavselect @close="handle_close" v-model="location"></sidenavselect>
</sidenav>
</div>
你想一次做太多事情。采取更小的步骤。更容易发现错误。
推荐阅读
- vba - 重命名包含特定文本的形状
- c# - 为什么这些整数没有达到上限?
- laravel - 统计laravel中的数据慢使用关系?
- c# - 如何在默认域的 Web.config 中同时启用基本和匿名身份验证?
- spring-boot - 在 Spring Boot 中,我正在尝试创建另一个功能类似于 messages.property 的 .property 文件,这可能吗?
- node.js - MongoDB根据数据更新整个集合
- keras - 加载的模型返回随机结果
- javascript - 用 JavaScript 计算复选框的值
- kotlin - 带有大量协程的 Kotlin 应用程序被锁定
- rocketmq - RocketMQ消息发出延迟消息,此后一直没有消息