把全局对象改为只读模式

master
yutent 2023-08-14 22:20:16 +08:00
parent 409ba1905e
commit e782d9cf96
3 changed files with 21 additions and 4 deletions

View File

@ -4,6 +4,15 @@
* @date 2023/08/10 10:19:12
*/
import { hideProp } from './utils.js'
const __WKITD__ = new Map()
hideProp(window, '__wkitd__', new Map())
Object.defineProperty(window, '__wkitd__', {
get() {
return __WKITD__
},
set(val) {
console.error('Can not set readonly property __wkitd__ of window')
},
writable: false,
enumerable: false
})

View File

@ -19,7 +19,15 @@ export function createRouter({
$router.addRoute(routes)
function wrapper() {
Component.prototype.$router = $router
Object.defineProperty(Component.prototype, '$router', {
get() {
return window.__wkitd__.get('$router')
},
set(val) {
console.error('Can not set readonly property $router of Component')
},
enumerable: false
})
}
wrapper.beforeEach = $router.beforeEach.bind($router)
return wrapper

View File

@ -23,7 +23,7 @@ export function createStore(obj = {}) {
return window.__wkitd__.get('$store')
},
set(val) {
console.error('Can not replace readonly property $store of Component')
console.error('Can not set readonly property $store of Component')
},
enumerable: false
})