5***@qq.com
5***@qq.com
  • 发布:2024-10-10 10:25
  • 更新:2024-10-10 11:03
  • 阅读:92

type 类型数组 在页面v-for报错,android

分类:uni-app x

export type RouteInfo = {
name : string,
title : string,
icon : string,
path : string,
permId : string,
roleId : number,
children : RouteInfo[]
}

export function getRoute() : RouteInfo[] {
const tmpRoute = SysRoutes as RouteInfo[];
// const roleType = getRoleType();
return tmpRoute
}

-- 页面:
<template>
<scroll-view :scroll-y="true" class="home-page">
<scroll-view class="left-route" :scroll-y="true">
<view class="left-oute-item" v-for="(route,index) in routes" :key="index">
<text>{{route.title}}</text>
</view>
</scroll-view>
<scroll-view class="right-route" :scroll-y="true">

    </scroll-view>  
</scroll-view>  

</template>
<script lang="uts">
import { getRoute } from '@/store/routers.uts'
import { RouteInfo } from '@/types/routeInfo'
export default {
data() {
return {
routes: [] as Array<RouteInfo>
}
},
created() {
this.routes = getRoute() as RouteInfo[];
},
onShow() { },
methods: {
}
}
</script>

-- 编译控制台报错:
10:18:49.021 ‌error: java.lang.ClassCastException: io.dcloud.uniapp.vue.UTSReactiveJSONObject cannot be cast to uni.UNI791BB6D.RouteInfo‌
10:18:49.021 at pages/index/home.uvue:4:3
10:18:49.021 2 | <scroll-view :scroll-y="true" class="home-page">
10:18:49.021 3 | <scroll-view class="left-route" :scroll-y="true">
10:18:49.021 4 | <view class="left-oute-item" v-for="(route,index) in routes" :key="index">
10:18:49.021 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
10:18:49.021 5 | <text>{{route.title}}</text>
10:18:49.021 6 | </view>

2024-10-10 10:25 负责人:无 分享
已邀请:
DCloud_UNI_yuhe

DCloud_UNI_yuhe

你好,我根据你给出的代码做出了如下复现代码,没有发现报错

<template>  
    <scroll-view :scroll-y="true" class="home-page">  
        <scroll-view class="left-route" :scroll-y="true">  
            <view class="left-oute-item" v-for="(route,index) in routes" :key="index">  
                <text>{{route.title}}</text>  
            </view>  
        </scroll-view>  
        <scroll-view class="right-route" :scroll-y="true">  
        </scroll-view>  
    </scroll-view>  
</template>  

<script lang="uts">  
    type RouteInfo = {  
        name : string,  
        title : string,  
        icon : string,  
        path : string,  
        permId : string,  
        roleId : number,  
        children : RouteInfo[]  
    }  

    const SysRoutes : RouteInfo[] = [  
        {  
            name: "yuhe1",  
            title: "yuhe2",  
            icon: "icon-home",  
            path: "/home",  
            permId: "perm1",  
            roleId: 1,  
            children: [  
                {  
                    name: "yuhe-child1",  
                    title: "yuhe-child2",  
                    icon: "icon-child",  
                    path: "/home/child",  
                    permId: "perm2",  
                    roleId: 2,  
                    children: []  
                }  
            ]  
        }  
    ];  
    function getRoute() : RouteInfo[] {  
        const tmpRoute = SysRoutes as RouteInfo[];  
        // const roleType = getRoleType();  
        return tmpRoute  
    }  

    export default {  
        data() {  
            return {  
                routes: [] as Array<RouteInfo>  
            }  
        },  
        created() {  
            this.routes = getRoute() as RouteInfo[];  
        },  
        onShow() { },  
        methods: {  
        }  
    }  
</script>
  • 5***@qq.com (作者)

    尴尬了,我不知道哪里出了问题,我把RouteInfo[]更改为UTSJSONObject[] 就没报错,在项目中用type类型就报错,而且项目编译特别慢

    2024-10-10 11:09

  • DCloud_UNI_yuhe

    回复 5***@qq.com: 你尝试给一下能稳定复现报错的代码

    2024-10-10 11:12

要回复问题请先登录注册