【发布时间】:2016-07-14 00:57:24
【问题描述】:
我有一个类似于此视频中的警报组件:https://laracasts.com/series/learning-vue-step-by-step/episodes/21 我还有另一个组件(书)。创建一本书时,如何在成功回调函数中调用 Alert 组件,如下所示:
<alert>A book was created successfully !!!</alert>
我是使用 vue.js 的新手。感谢您的帮助。
更新:这是我的代码
submit: function () {
this.$http.post('/api/books/add', {
data: this.data,
}).then(function (response) {
// I want to use Alert component right here to notice to users.
}, function (response) {
});
}
更新 2: 警报组件
<template>
<div class="Alert Alert--{{ type | capitalize }}"
v-show="show"
transition="fade"
>
<slot></slot>
<span class="Alert__close"
v-show="important"
@click="show = false"
>
x
</span>
</div>
</template>
<script>
export default {
props: {
type: { default: 'info' },
timeout: { default: 3000 },
important: {
type: Boolean,
default: false
}
},
data() {
return {show: true};
},
ready() {
if (!this.important)
{
setTimeout(
() => this.show = false,
this.timeout
)
}
}
}
</script>
<style>
.Alert {
padding: 10px;
position: relative;
}
.Alert__close {
position: absolute;
top: 10px;
right: 10px;
cursor: pointer;
}
.Alert--Info {
background: #e3e3e3;
}
.fade-transition {
transition: opacity 1s ease;
}
.fade-leave {
opacity: 0;
}
</style>
在 Book.vue 中我想这样做:
// resources/assets/js/components/Book.vue
<template>
.....
<alert>A book was created successfully !!!</alert>
//Create book form
....
</template>
<script>
export default {
methods: {
submit: function () {
this.$http.post('/api/books/add', {
data: this.data,
}).then(function (response) {
this.$refs.alert
}, function (response) {
});
}
</script>
【问题讨论】:
-
注意添加一些代码...我可以告诉你有不止一种方法可以做到这一点。您的解决方案将取决于您的代码。无论你拥有什么......都会奏效。
标签: javascript vue.js vue-resource