用原生JS模拟Vue的State插件的写法

2020-04-24 14:53:24 浏览数 (1)

代码语言:javascript复制
<html>

<body>
  <input type="text" id="myinput">
</body>
<script>
  var myinput = document.querySelector('#myinput');

  function Vue() {

  }

  Vue.Use = function (F, options) {
    F(this, options);
  }


  var VueState = function install(VueObj, state) {
    Object.defineProperty(VueObj.prototype, '$state', {
      get: () => state
    })
  };


  Vue.Use(VueState, {
    age: 18,
    name: 'Liu Yi'
  });


  var vue = new Vue();

  myinput.value = vue.$state.name;
  console.log('myinput.value', myinput.value);
</script>

</html>

0 人点赞