IT

Vue global method 전역 함수 설정. for preventing javascript nested object null

bepuri 2020. 11. 13. 12:45
728x90

[@/plugins/get_safe.js]

..

const getSafe = {

  install(Vue){

    Vue.prototype.$get = function(objkey) {

      return key.split(".").reduce(function(ox) {

          return (typeof o == "undefined" || o === null) ? o : o[x];

      }, obj);

    }

  }

}

 

export default getSafe;

 

[main.js]

import getSafe from "@/plugins/get_safe";

...

 

Vue.use(getSafe);

 

이렇게하면 nested object가 null이더라도 에러가 나지 않고 페이지가 정상적으로 호출된다.

다만 데이터가 비어있을뿐.

이와 같은 코드는 좋은 코드는 아니다. 왜냐면 데이터로 인해 생기는 버그가 backtrace하기가 힘들기 때문이다.

 

728x90