vue3 在插槽里面用父组件传过来的数据的属性引用遇到语法检测: unknown,怎么修改。 - V2EX
slmakm

vue3 在插槽里面用父组件传过来的数据的属性引用遇到语法检测: unknown,怎么修改。

  •  
  •   slmakm Oct 15, 2022 2414 views
    This topic created in 1311 days ago, the information mentioned may be changed or developed.

    这个不是我的代码我也看不懂,所以想问问大家怎么解决?我在想是不是跟默认插槽有关 下方会放父组件和子组件的代码:

    父组件:

    <template> <div class="user"> <page-search :searchFormCOnfig="searchFormConfig" /> <div class="content"> <hy-table :listData="userList" :propList="propList"> <template #status="scope"> <el-button>{{ scope.row.enable ? "启用" : "禁用" }}</el-button> </template> <template #createAt="scope"> <strong>{{ scope.row.createAt }}</strong> </template> </hy-table> </div> </div> </template> <script lang="ts"> import { defineComponent, computed } from "vue"; //引入搜索模板 import PageSearch from "@/components/page-search"; //引入 user 组件的数据和配置 import { searchFormConfig } from "./config/search.config"; import { useStore } from "@/store"; import hyTable from "@/base-ui/table"; export default defineComponent({ name: "user", components: { PageSearch, hyTable }, setup() { const userList = computed(() => store.state.system.userList); const userCount = computed(() => store.state.system.userCount); const propList = [ { prop: "name", label: "用户名", minWidth: "100" }, { prop: "realname", label: "真实姓名", minWidth: "100" }, { prop: "cellphone", label: "电话号码", minWidth: "100" }, { prop: "enable", label: "状态", minWidth: "100", slotName: "enable" }, { prop: "createAt", label: "创建时间", minWidth: "250", slotName: "createAt" }, { prop: "updateAt", label: "更新时间", minWidth: "250", slotName: "updateAt" } ]; return { searchFormConfig, userList, propList }; } }); </script> <style scoped> .content { padding: 20px; border-top: 20px solid #f5f5f5; } </style> 

    子组件

    <template> <div class="hy-table"> <el-table :data="listData" border style="width: 100%"> <template v-for="propItem in propList" :key="propItem.prop"> <el-table-column v-bind="propItem" align="center"> <template #default="scope"> <slot :name="propItem.slotName" :row="scope.row"> {{ scope.row[propItem.prop] }} </slot> </template> </el-table-column> </template> </el-table> </div> </template> <script lang="ts"> import { defineComponent } from "vue"; export default defineComponent({ props: { listData: { type: Array, require: true }, propList: { type: Array, require: true } }, setup() { return {}; } }); </script> <style scoped></style> 
    8 replies    2022-10-15 20:34:52 +08:00
    slmakm
        1
    slmakm  
    OP
       Oct 15, 2022
    忘了说一句可以运行成功,在运行过程中命令行不会提示错误。是跟代码检测有关吗?在 vscode 里安装了 eslint
    shakukansp
        2
    shakukansp  
       Oct 15, 2022
    给 unknown 的对象定个类型
    unknown 一个特点就是你不给类型就不能继续往下.xxx 读取属性
    chenluo0429
        3
    chenluo0429  
       Oct 15, 2022
    你的 propList 类型是 unknown[],你需要使用写成 type: Array as PropType<YourType[]>
    或者使用 script setup 的 defineProps 来定义 prop
    chenjiangui998
        4
    chenjiangui998  
       Oct 15, 2022
    propList 类型是 unknown[], 想省事情就 propList: {
    type: Array as PropType<any[]>,
    require: true
    }

    正确做法是正确定义类型, 不定义还不如 js , 还有 ts 直接用 setup 模式, 用啥 defineComponent
    creanme
        5
    creanme  
       Oct 15, 2022
    ```
    <script lang="ts" setup>
    const props = defineProps({
    listData: Array<any>,
    propList: Array<{
    slotName: string;
    prop: string;
    }>,
    });
    </script>
    ```
    slmakm
        6
    slmakm  
    OP
       Oct 15, 2022
    @chenjiangui998 谢谢你,你写法和想法都是对的,我已经按照你的代码超过去后错误提示它就没了。我这个子组件是通用模板,不能直接写死。所以只能 PropType<any[]>
    slmakm
        7
    slmakm  
    OP
       Oct 15, 2022
    @chenjiangui998 引用:"正确做法是正确定义类型, 不定义还不如 js , 还有 ts 直接用 setup 模式, 用啥 defineComponent"
    我在跟着一个老项目来学,老师用的 vue 版本是 3.1.x 你说的这些都是那时候的新特性还没有引入到 vue 正式版本。是从 3.2.x 开始正式用的,老师说最后会用新特性。谢谢你的提醒
    slmakm
        8
    slmakm  
    OP
       Oct 15, 2022
    @creanme 我最近在学 vue3 ,而且跟着一个老项目跟着做,你这个新特老师提过,但还没有正式用。谢谢你的回复
    About     Help     Advertise     Blog     API     FAQ     Solana     3021 Online   Highest 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 42ms UTC 12:58 PVG 20:58 LAX 05:58 JFK 08:58
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86