Coder Social home page Coder Social logo

mini_vue's Introduction

模拟vue进行封装,实现MVVM,同时实现了v-if,v-show,v-on,v-text,v-bind,v-model,模板字符串等指令
实现了beforeCreate,created,beforeMount,mounted生命周期
参考axios思路进行封装ajax,方便快速发起http请求
以及封装了websocket,快速发送消息,处理消息,以及断线自动重连并将它们全部整合


vue构造函数

function Vue(options) {
  // 执行beforeCreate生命周期
  if (options.beforeCreate) {
    options.beforeCreate.call(this);
  }

  // 给this添加代理
  this.$vm = this._proxy(this);
  let _this = this.$vm;

  _this.$options = options || {};
  _this.$data =
    typeof options.data === "function" ? options.data() : options.data;

  // 初始化data数据
  this.initData(_this, _this.$data);

  // 初始化methods,改变函数this指向同时挂载到实例上面
  this.initMethods(_this, options.methods);

  // 执行created生命周期
  if (options.created) {
    options.created.call(_this);
  }

  // 执行beforeMount生命周期
  if (options.beforeMount) {
    options.beforeMount.call(_this);
  }

  // 挂载节点
  _this.$el = document.getElementById(options.el);
  // 解析模板 并创建订阅发布者
  new Compiler(_this);

  // 执行mounted生命周期
  if (options.mounted) {
    options.mounted.call(_this);
  }

  return _this;
}

详细代码请下载查看

mini_vue's People

Contributors

cn-huan avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.