陶杰
2024-08-22 4aa0bd47801606b4d0e0a6a2ed8fe92a7e5f2444
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
<template>
    <view class="uni-breadcrumb-item">
        <view :class="{
            'uni-breadcrumb-item--slot': true,
            'uni-breadcrumb-item--slot-link': to && currentPage !== to
            }" @click="navTo">
            <slot />
        </view>
        <i v-if="separatorClass" class="uni-breadcrumb-item--separator" :class="separatorClass" />
        <text v-else class="uni-breadcrumb-item--separator">{{ separator }}</text>
    </view>
</template>
<script>
    /**
     * BreadcrumbItem 面包屑导航子组件
     * @property {String/Object} to 路由跳转页面路径/对象
     * @property {Boolean} replace 在使用 to 进行路由跳转时,启用 replace 将不会向 history 添加新记录(仅 h5 支持)
     */
    export default {
        data() {
            return {
                currentPage: ""
            }
        },
        options: {
            virtualHost: true
        },
        props: {
            to: {
                type: String,
                default: ''
            },
            replace:{
                type: Boolean,
                default: false
            }
        },
        inject: {
            uniBreadcrumb: {
                from: "uniBreadcrumb",
                default: null
            }
        },
        created(){
            const pages = getCurrentPages()
            const page = pages[pages.length-1]
 
            if(page){
                this.currentPage = `/${page.route}`
            }
        },
        computed: {
            separator() {
                return this.uniBreadcrumb.separator
            },
            separatorClass() {
                return this.uniBreadcrumb.separatorClass
            }
        },
        methods: {
            navTo() {
                const { to } = this
 
                if (!to || this.currentPage === to){
                    return
                }
 
                if(this.replace){
                    uni.redirectTo({
                        url:to
                    })
                }else{
                    uni.navigateTo({
                        url:to
                    })
                }
            }
        }
    }
</script>
<style lang="scss">
    $uni-primary: #2979ff !default;
    $uni-base-color: #6a6a6a !default;
    $uni-main-color: #3a3a3a !default;
    .uni-breadcrumb-item {
        display: flex;
        align-items: center;
        white-space: nowrap;
        font-size: 14px;
 
        &--slot {
            color: $uni-base-color;
            padding: 0 10px;
 
            &-link {
                color: $uni-main-color;
                font-weight: bold;
                /* #ifndef APP-NVUE */
                cursor: pointer;
                /* #endif */
 
                &:hover {
                    color: $uni-primary;
                }
            }
        }
 
        &--separator {
            font-size: 12px;
            color: $uni-base-color;
        }
 
        &:first-child &--slot {
            padding-left: 0;
        }
        
        &:last-child &--separator {
            display: none;
        }
    }
</style>