booleen
booleen
  • 发布:2025-03-15 23:18
  • 更新:2025-03-16 17:01
  • 阅读:75

const xx=ref<State>({yy:x}) xx.value 类型不等于State吗?

分类:uni-app x
const xx=ref<State>({yy:x})  xx.value 类型不等于State吗?  

type State ={  
yy:string;  
}  
type ResultObj={  
   state:State;  
}  
const xx=ref<State>({yy:'xx'})    

function result():ResultObj{  

  return {  
    state:xx.value  

   } as ResultObj  

}  

**
异常:

error: java.lang.ClassCastException: io.dcloud.uniapp.vue.UTSReactiveJSONObject cannot be cast to uni.UNIC2451D5.State‌**

2025-03-15 23:18 负责人:无 分享
已邀请:
DCloud_UNI_FengXY

DCloud_UNI_FengXY

确保已使用最新的HBuilderX,上述贴的代码运行没有问题

  • booleen (作者)

    //这个是其他文件导入的函数,只要是用来更新对象,我想抽出来

    export function UtilSetState<T>(options:SetStateArgs):T{

    const {objValue ,state,setCache,localKey}=options;

    for (let key in objValue) {

    state[key] = objValue[key];

    }

    if(setCache==true){

    setLocalStore(state,localKey)

    }

    //这里这个代码导致的!!!!!!!!!!!!!!!!,这有什么方法解决吗

    return state as T;

    }


    const xx=ref<State>({yy:x}) xx.value 类型不等于State吗?


    type State ={

    yy:string;

    }

    type ResultObj={

    state:State;

    }

    const xx=ref<State>({yy:'xx'})


    function setState(obj:UTSJSONObject){

    const copyState=JSON.parse<UTSJSONObject>(JSON.stringify(state.value))!;

    state.value=UtilSetState<State>({

    state:copyState,

    objValue:obj,

    setCache:openLocalStore,

    localKey:localKey

    } as SetStateArgs);

    }


    function result():ResultObj{


    return {

    state:xx.value


    } as ResultObj


    }

    2025-03-16 23:19

  • booleen (作者)

    报错定位不准确,这种情况,定位到。function result():ResultObj{


    return {


    state:xx.value


    } as ResultObj


    }这个函数最后一行了,期待下一版本解决这个提示问题

    2025-03-16 23:20

  • DCloud_UNI_FengXY

    回复 booleen:

    const copyState = JSON.parse<UTSJSONObject>(JSON.stringify(state.value))!;

    解析出来的是UTSJSONObject,是不能赋值给State类型的。你应该解析成State类型:

    JSON.parse<State>(JSON.stringify(state.value))!;

    报错位置的问题:因为你给的代码都是片段,没法排查。

    2025-03-17 11:03

要回复问题请先登录注册