可能有几种方法可以做到这一点,我会创建一个plugin。
那么你就有了<snackbar/> 用于放置的组件和一个全局API 来调用调用方法this.$snackbar.open({someOptions: '...'})
例如:
在./plugins/snackbar 中创建一个文件夹并将以下内容放入:
./plugins/snackbar/index.js
import Vue from "vue";
import snackbar from "~/plugins/snackbar/snackbar";
Vue.use(snackbar);
这是为nuxt.config.js 全局加载的。看起来像:
...
/*
** Plugins to load before mounting the App
** Doc: https://nuxtjs.org/guide/plugins
*/
plugins: ["~/plugins/snackbar/index.js"],
...
好的,现在创建
./plugins/snackbar/snackbar.js
这是保存组件状态并充当事件代理的插件
import snackbar from "~/plugins/snackbar/snackbar.vue";
const Plugin = {
install(Vue, options = {}) {
/**
* Makes sure that plugin can be installed only once
*/
if (this.installed) {
return;
}
this.installed = true;
/**
* Create event bus
*/
this.event = new Vue();
/**
* Plugin methods
*/
Vue.prototype.$snackbar = {
show(options = {}) {
Plugin.event.$emit("show", options, true);
}
};
/**
* Registration of <snackbar/> component
*/
Vue.component("snackbar", snackbar);
}
};
export default Plugin;
现在……
./plugins/snackbar/snackbar.vue
魔法发生的地方......
<template>
<div>
<transition name="snackbar">
<div v-if="show" :class="['snackbar', 'box-shadow', type]">
<slot>{{ options.text }}</slot>
</div>
</transition>
<pre>options: {{ options }}</pre>
<pre>show: {{ show }}</pre>
<pre>type: {{ type }}</pre>
</div>
</template>
<script>
import snackbar from "~/plugins/snackbar/snackbar";
export default {
data: () => ({
options: {
text: "",
type: ""
},
show: false,
type: "",
timer: 0
}),
beforeMount() {
snackbar.event.$on("show", options => {
this.options = options;
this.type = options.type;
this.show = true;
this.close(this.options.closeWait || 3000);
});
},
methods: {
close(timeout) {
clearTimeout(this.timer);
this.timer = setTimeout(() => {
this.show = false;
}, timeout);
}
}
};
</script>
<style>
.snackbar {
min-width: 300px;
margin-left: -150px;
background-color: #F48024;
color: #fff;
text-align: center;
border-radius: 5px;
padding: 16px;
position: fixed;
z-index: 1;
left: 50%;
bottom: 30px;
}
.snackbar.success {
background-color: rgb(71, 244, 36);
}
.snackbar.danger {
background-color: rgb(244, 36, 47);
}
.snackbar-enter-active {
animation: snackbar-in 0.8s;
}
.snackbar-leave-active {
animation: snackbar-in 0.8s reverse;
}
@keyframes snackbar-in {
0% {
transform: scale(0);
}
50% {
transform: scale(1.2);
}
100% {
transform: scale(1);
}
}
.box-shadow {
-webkit-box-shadow: 0 1px 4px rgba(0, 0, 0, 0.3),
0 0 40px rgba(0, 0, 0, 0.1) inset;
-moz-box-shadow: 0 1px 4px rgba(0, 0, 0, 0.3),
0 0 40px rgba(0, 0, 0, 0.1) inset;
box-shadow: 0 1px 4px rgba(0, 0, 0, 0.3), 0 0 40px rgba(0, 0, 0, 0.1) inset;
}
</style>
然后在使用它的任何组件/页面中,您可以使用<snackbar/> 放置,并调用如下方法:
this.$snackbar.show({
text: "Hello, snackbar!",
type: "success"
});
上面的一个工作示例可以在这里找到https://codesandbox.io/s/codesandbox-nuxt-oeo4h