關(guān)于vue中使用three.js報錯的解決方法
前言
最近在學習three.js,同時也學習一下vue3,然后就出現(xiàn)問題了,報錯直接用不了,錯誤信息如下:

Uncaught TypeError: 'get' on proxy: property 'modelViewMatrix' is a read-only and non-configurable data property on the proxy target but the proxy did not return its actual value (expected '#' but got '[object Object]')
這個是什么鬼???相信大家都把錯誤信息復制到百度搜了一下解決方案吧?遇到問題的人并不多,解決方案就是把scene、mesh 啥的放到全局變量中,不放到data中,好的問題解決了。
我這樣寫是不是有點水文的嫌疑?作為一個成熟的程序員怎么能水文呢?我得找到為什么會出現(xiàn)這個問題才行,于是我花了兩個小時找到問題所在,也是怪自己不夠?qū)I(yè),不然應該花不了兩個小時。
1. vue的問題?
眾所周知,vue3是通過Proxy實現(xiàn)的數(shù)據(jù)雙向綁定,vue2是通過defindeProperty實現(xiàn)的數(shù)據(jù)雙向綁定,vue2的源碼我也看過,應該也是會有這個問題的,因為在開發(fā)模式下面,如果瀏覽器支持Proxy還是會用Proxy,我沒有用vue2去嘗試,大家可以自行去嘗試,不出意外也會有這個問題。
上面說了一堆,就是為了引出Proxy的異常情況,好了又可以學習一下Proxy的知識了
2. Proxy的異常情況
在使用Proxy時,當屬性存在屬性特性configurable: false, value: undefined,時,則取其屬性值時會報錯:
const handle = {
get() {
return 1;
},
};
const obj = Object.create(null);
Object.defineProperty(obj, 'a', {
configurable: false,
});
Object.defineProperty(obj, 'b', {
value: undefined,
});
Object.defineProperty(obj, 'c', {
configurable: true,
value: 'c',
});
const proxy = new Proxy(obj, handle);
console.log(proxy.a); // 報錯TypeError: 'get' on proxy: property 'a' is a read-only and non-configurable data property on the proxy target but the proxy did not return its actual value (expected 'undefined' but got '1')
console.log(proxy.b); // 報錯Uncaught TypeError: 'get' on proxy: property 'b' is a read-only and non-configurable data property on the proxy target but the proxy did not return its actual value (expected 'undefined' but got '1')
console.log(proxy.c); // 1看看上面的報錯,是不是很熟悉?這樣看是不是一下就知道是什么問題了?
3. Three.js 的問題
this.scene = new Scene(); this.scene.modelViewMatrix;
直接運行上面的代碼,就會看到文首出現(xiàn)的錯誤,錯誤原因就是因為configurable設(shè)置為false,找到問題就要解決問題,是不是覺得直接使用defineProperty就可以解決了?
很抱歉,這個不行,再來學習一下defineProperty。
4. defineProperty異常情況
MDN中是這樣描述的
如果屬性已經(jīng)存在,
Object.defineProperty()將嘗試根據(jù)描述符中的值以及對象當前的配置來修改這個屬性。如果舊描述符將其configurable 屬性設(shè)置為false,則該屬性被認為是“不可配置的”,并且沒有屬性可以被改變(除了單向改變 writable 為 false)。當屬性不可配置時,不能在數(shù)據(jù)和訪問器屬性類型之間切換。
當試圖改變不可配置屬性(除了 value 和 writable 屬性之外)的值時,會拋出TypeError,除非當前值和新值相同。
也就是說之前定義了configurable為false,就不能再將configurable改為true了,那怎么辦?我說了問題當然是要給你解決的。
5. 解決
之前在網(wǎng)上查了,全局變量來處理,但是我使用的vue啊,我當然是希望將它定義到data中的,但是定義到data中就會自動生成代理,那就只能從源碼入手了。 我也就不講我是怎么去找源碼的,我直接上解決之后的吧,在node_modules\three\build\three.module.js這個文件中,第7392行,里面的代碼如下:
Object.defineProperties( this, {
position: {
configurable: true,
enumerable: true,
value: position
},
rotation: {
configurable: true,
enumerable: true,
value: rotation
},
quaternion: {
configurable: true,
enumerable: true,
value: quaternion
},
scale: {
configurable: true,
enumerable: true,
value: scale
},
modelViewMatrix: {
value: new Matrix4()
},
normalMatrix: {
value: new Matrix3()
}
} );看到了吧,里面有一個modelViewMatrix屬性,它沒有設(shè)置configurable屬性描述,也就是默認為false,加上就好了,改好了如下:
Object.defineProperties( this, {
position: {
configurable: true,
enumerable: true,
value: position
},
rotation: {
configurable: true,
enumerable: true,
value: rotation
},
quaternion: {
configurable: true,
enumerable: true,
value: quaternion
},
scale: {
configurable: true,
enumerable: true,
value: scale
},
modelViewMatrix: {
configurable: true,
value: new Matrix4()
},
normalMatrix: {
value: new Matrix3()
}
} );然后重啟服務,就不會報錯了,當然這種方式是有缺陷的,因為改了只是你本地的,你其他同事的代碼并沒有改,如果要升級three.js也會把改了的代碼重新覆蓋,同時也不知道為什么three.js要這樣處理這個變量,這個就看自己是怎么處理的,我這次只處理這個問題。
總結(jié)
到此這篇關(guān)于vue中使用three.js報錯解決的文章就介紹到這了,更多相關(guān)vue使用three.js報錯內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue3路由跳轉(zhuǎn)params傳參接收不到的解決辦法
這篇文章主要給大家介紹了關(guān)于vue3路由跳轉(zhuǎn)params傳參接收不到的解決辦法,Vue3是目前前端開發(fā)中非常流行的框架之一,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下2023-06-06
解決vue3+vite配置unplugin-vue-component找不到Vant組件
這篇文章主要為大家介紹了vue3+vite配置unplugin-vue-component找不到Vant組件問題解決,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-09-09
vue使用elementui的el-menu的折疊菜單collapse示例詳解
這篇文章主要介紹了vue使用elementui的el-menu的折疊菜單collapse示例詳解,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-12-12
Vue利用History記錄上一頁面的數(shù)據(jù)方法實例
這篇文章主要給大家介紹了關(guān)于Vue利用History記錄上一頁面的數(shù)據(jù)的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2018-11-11

