3***@qq.com
3***@qq.com
  • 发布:2023-10-27 14:43
  • 更新:2023-10-28 04:15
  • 阅读:480

为什么报错呢?

分类:uni-app x
<template>  
    <view class="tabbar">  
        <view class="page">  
            4444  
        </view>  
        <view class="tabbar-bar">  
            <view class="item" v-for="(item,index) in list" :key="index">  
                <view class="icon">{{title}}</view>  
                <view class="text"><text>{{item.age}}</text></view>  
            </view>  
        </view>  
    </view>  
</template>  

<script>  
    export default {  
        data() {  
            return {  
                title: "hello uni-app x",  
                list:[{age:18},{age:11}]  
            }  
        },  
        onLoad() {},  
        methods: {  

        }  
    }  
</script>  

<style scoped>  
    .tabbar {  
        width: 750rpx;  
        flex: 1;  
    }  

    .page {  
        width: 750rpx;  
        flex: 1;  
        background-color: purple;  
    }  

    .tabbar-bar {  
        width: 750rpx;  
        height: 100px;  
        background-color: green;  

    }  
</style>  

报错信息如下
14:39:08.001 [plugin:uni:app-uts] 编译失败
14:39:08.005 ‌error: Unresolved reference: age‌
14:39:08.005 at pages/tabbar/tabbar.uvue:9:36
14:39:08.005 7 | <view class="item" v-for="(item,index) in list" :key="index">
14:39:08.005 8 | <view class="icon">{{title}}</view>
14:39:08.005 9 | <view class="text"><text>{{item.age}}</text></view>
14:39:08.005 | ^
14:39:08.005 10 | </view>
14:39:08.005 11 | </view>

2023-10-27 14:43 负责人:无 分享
已邀请:
爱豆豆

爱豆豆 - 办法总比困难多

要定义类型

<template>    
    <view class="tabbar">    
        <view class="page">    
            4444    
        </view>    
        <view class="tabbar-bar">    
            <view class="item" v-for="(item,index) in list" :key="index">    
                <view class="icon">{{title}}</view>    
                <view class="text"><text>{{item.age}}</text></view>    
            </view>    
        </view>    
    </view>    
</template>    

<script lang="uts">    
    type User = {  
        age:number  
    } //定义一个类型  
    export default {    
        data() {    
            return {    
                title: "hello uni-app x",    
                list:[{age:18},{age:11}] as User[]  
            }    
        },    
        onLoad() {},    
        methods: {    

        }    
    }    
</script>    

<style scoped>    
    .tabbar {    
        width: 750rpx;    
        flex: 1;    
    }    

    .page {    
        width: 750rpx;    
        flex: 1;    
        background-color: purple;    
    }    

    .tabbar-bar {    
        width: 750rpx;    
        height: 100px;    
        background-color: green;    

    }    
</style>  
3***@qq.com

3***@qq.com (作者)

这又是为什么呢

<template>  
    <view class="tabbar">  
        <view class="page">  
            4444  
        </view>  
        <view class="tabbar-bar">  
            <view class="item" v-for="(item,index) in tabbar" :key="index">  
                <view class="icon">{{title}}</view>  
                <view class="text"><text>{{item.text}}</text></view>  
            </view>  
        </view>  
    </view>  
</template>  

<script lang="uts">  
    type pType = {  
        name : string  
        age : number  
    }  
    type tabbarType = {  
        pagePath : string  
        iconPath : string  
        selectedIconPath : string  
        text : string  
    }  
    export default {  
        data() {  
            return {  
                targetTabbars: [  
                    [{  
                        "pagePath": "home",  
                        "text": "首页"  
                    }  
                    ],  
                    [{  
                        "pagePath": "home",  
                        "text": "首页"  
                    }  
                    ]  

                ] as Array<Array<UTSJSONObject>>,  
                title: "hello uni-app x",  
                tabbar: [] as Array<UTSJSONObject>,  
                p: { 'name': 'xiaohei ', 'age': 18 } as pType,  

            }  
        },  
        onLoad() {  
            this.tabbar = this.targetTabbars[0]  
        },  
        methods: {  

        }  
    }  
</script>  

<style scoped>  
    .tabbar {  
        width: 750rpx;  
        flex: 1;  
    }  

    .page {  
        width: 750rpx;  
        flex: 1;  
        background-color: purple;  
    }  

    .tabbar-bar {  
        width: 750rpx;  
        height: 100px;  
        background-color: green;  

    }  
</style>

错误信息如下
16:49:59.580 [plugin:uni:app-uts] 编译失败
16:49:59.585 ‌error: Unresolved reference: text‌
16:49:59.585 at pages/tabbar/tabbar.uvue:9:36
16:49:59.585 7 | <view class="item" v-for="(item,index) in tabbar" :key="index">
16:49:59.585 8 | <view class="icon">{{title}}</view>
16:49:59.585 9 | <view class="text"><text>{{item.text}}</text></view>
16:49:59.585 | ^
16:49:59.585 10 | </view>
16:49:59.585 11 | </view>

  • 3***@qq.com

    {{item.text}}改为{{item['text']}}

    2023-10-27 18:04

  • 3***@qq.com

    UTSJSONObject是uts的内置对象,它无法使用.操作符,但可以用下标和keypath来访问json数据。

    这里是文档地址:https://uniapp.dcloud.net.cn/uni-app-x/tutorial/request.html

    2023-10-27 18:11

  • 3***@qq.com (作者)

    回复 3***@qq.com: 还是报错18:29:34.074 11 | </view>

    18:29:34.074 ‌error: No get method providing array access‌

    18:29:34.074 at pages/tabbar/tabbar.uvue:9:35

    18:29:34.074 7 | <view class="item" v-for="(item,index) in tabbar" :key="index">

    18:29:34.074 8 | <view class="icon">{{title}}</view>

    18:29:34.074 9 | <view class="text"><text>{{item["text"]}}</text></view>

    18:29:34.076 | ^

    18:29:34.076 10 | </view>

    18:29:34.076 11 | </view>

    2023-10-27 18:30

DCloud_heavensoft

DCloud_heavensoft

要回复问题请先登录注册