Skip to content

TagsNav - 标签导航

基本用法

显示代码
jsx
import { defineComponent, ref, unref } from 'vue'
import { Button } from 'ant-design-vue'
import { TagsNav } from '@/packages/index.js'

export default defineComponent({
    setup () {
        let key = 3

        const route = ref({
            name: 'menu-1',
            meta: { title: '菜单一' }
        })


        const tags = ref([
            {
                name: 'menu-1',
                meta: { title: '菜单-1' }
            },
            {
                name: 'menu-2',
                meta: { title: '菜单-2' }
            },
            {
                name: 'menu-3',
                meta: { title: '菜单-3' }
            },
        ])

        function onClick (current) {
            route.value = current
        }

        function onClose (values, toName) {
            tags.value = values
            if (toName) {
                const value = unref(values).find((item) => {
                    return toName === item.name
                })
                if (value) {
                    route.value = value
                }
            }
        }

        function onAddClick () {
            key += 1
            const next = {
                name: `menu-${key}`,
                meta: { title: `菜单-${key}` }
            }
            tags.value = [...unref(tags), next]
        }

        return () => {
            const tagsNavProps = {
                homeName: 'menu-1',
                route: unref(route),
                tags: unref(tags),
                onClick: onClick,
                onClose: onClose
            }

            return (
                <div>
                    <Button style={{ marginBottom: '24px' }} onClick={onAddClick}>添加标签</Button>
                    <TagsNav {...tagsNavProps}/>
                </div>
            )
        }
    }
})

API

属性

属性说明类型默认值
homeName保留的标签string-
route当前路由Route-
tags展示的标签Route[][]

事件

事件名称说明回调参数
onClick标签点击回调function(current)
onClose数据变化时回调function(values, name?)