StackDrawer 是一个可以无限push 任意vue组件,并挂载组件 的测弹窗组件; 支持返回、自定义容器、显示隐藏、遮罩层...
import StackDrawer from '@/components/StackDrawer';
const MyComponent = {
template:`<div class="test-com">
<ul>
<li
v-for="li in list"
:key="li"
class="li"
@click="clickOne(li)"
>{{ li }}</li>
</ul>
</div>`,
props: {
list: Array
},
setup(props, { emit }) {
const clickOne = (da) => {
emit('test', da);
};
const getSomething = () => {
console.log('子组件 getSomething 方法被调用');
};
return { clickOne, getSomething };
}
}
// 基本使用
StackDrawer
.push(MyComponent, { list: [1,2,3,4] }, { mask: false })
.$on('test', (da) => {
console.log('test 事件触发',da)
})
.show(StackDrawer.getLength() < 2)
.then(() => {
console.log('show 完成');
});
// 修改子组件props
StackDrawer.$$list = ['modify1', 'modify2', 'modify3', 'modify4'];
// 调用子组件方法
StackDrawer.$$getSomething();
// 挂载事件
const fn = (da) => { console.log(da) };
StackDrawer.$on('test',fn);
// 卸载事件
StackDrawer.$off('test',fn)
// 打开 显示
StackDrawer.show();
// hide 隐藏
StackDrawer.hide();
// 关闭 会清空栈
StackDrawer.close();
可以为StackDrawer指定dom容器customWarp, 推入的组件将渲染在customWarp中
移除自定义容器后, 推入的组件将渲染在默认的dom中
3. push(component:Vue.ComponentOptions<Vue>, propsData:any, options:StackDrawerOptions = {}):StackDrawer
推入一个组件
component:vue组件;propsData:component的props数据;options:Drawer 配置(具体参数件下方表格); 返回 StackDrawer
属性名字 | 类型 | 是否必须 | 参数意义 | 默认值 |
---|---|---|---|---|
width | number | no | 测弹窗宽度,不设置时为组件撑开的宽度 | - |
customClass | string | no | 自定义类名 | - |
mask | boolean | no | 是否有遮罩层, 使用自定义容器 该属性无效 | false |
maskCloseAnimate | boolean | no | 遮罩层触发的关闭是否有动画, 使用自定义容器 该属性无效 | true |
pushStack | boolean | no | 是否入栈, 设置为false 组件不会记录在栈中,无法返回 | true |
keepEmit | boolean | no | 子组件非活跃状态下保持事件触发 | false |
store | VueX | no | store 状态管理仓库,可以传入宿主vue实例的 store | - |
可以设置一个全局的配置, push方法中options将合并globalOptions
eventName: 事件名字, fn: 事件回调函数, 返回 StackDrawer
eventName: 事件名字, fn: 事件回调函数, 返回 StackDrawer
num: 返回页数, animate: 是否有动画
animate: 是否有动画
animate: 是否有动画
animate: 是否有动画
// 修改子组件中的list数据
StackDrawer.$$list = ['modify1', 'modify2', 'modify3', 'modify4', 'modify5', 'modify6'];
// 调用子组件中的getSomething方法
StackDrawer.$$getSomething();
import { getStackDrawer } from '@/components/StackDrawer';
const otherStackDrawer = getStackDrawer(); // 工厂函数 构造多个StackDrawer
otherStackDrawer
.push(MyComponent, { list: [1,2,3,4] }, { mask: false })
.$on('test', (da) => {
console.log('test 事件触发',da)
})
.show(StackDrawer.getLength() < 2)
.then(() => {
console.log('show 完成');
});
StackDrawer
.push(MyComponent, { list: [1,2,3,4] }, { mask: false })
.$on('test', (da) => {
console.log('test 事件触发',da)
})
.show(StackDrawer.getLength() < 2)
.then(() => {
console.log('show 完成');
});
// test2 事件不会生效
StackDrawer.$on('test2',() => {
console.log('never');
})
2. StackDrawer 会调用组件的 activated 和 deactivated 生命周期, 当StackDrawer.push一个新的组件后,上一个组件将执行deactivated, 当调用当StackDrawer.goback, 当前组件将执行deactivated, 前一个组件将执行activated