首页 > 其他分享 >Vue在js中的使用思考,非脚手架

Vue在js中的使用思考,非脚手架

时间:2023-03-10 14:48:29浏览次数:45  
标签:funcs function Vue 函数 js window func 脚手架 today

var vm = new Vue({
  el: ".container", // 挂载点
  data: {}, // 数据决定了页面的样子 | 数据的响应式 => 数据变了界面跟着变
  computed: {}, // 计算属性,惰性求值的依赖者,存在缓存
})

  1. 在Vue单例中,一般会把整个页面交给Vue来控制。
  2. new Vue()中的data为啥可以是对象,因为其不会被复用
  3. 依赖,指的是使用到了这个属性,需要在get方法收集依赖。
  4. get函数运行时,依赖收集,记录是哪个函数在用我,创建内置数组(元素唯一)存放使用属性的函数。
  5. set函数执行时,派发更新,运行执行用我的函数,遍历内置数组执行里面的函数。
  6. 如何知道谁用到这个属性呢,在全局定义了一个属性,开始的时候也就是使用属性的时候,触发了get方法,往内置数组funcs加入更新函数func,当重新赋值的时候则遍历数组执行更新函数。
index.html
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <link rel="stylesheet" href="./index.css" />
  </head>
  <body>
    <div class="card">
      <p id="firstName"></p>
      <p id="lastName"></p>
      <p id="age"></p>
    </div>
    <input type="text" oninput="user.name = this.value" />
    <input type="date" onchange="user.birth = this.value" />
    <script src="./euv.js"></script>
    <script src="./index.js"></script>
  </body>
</html>

index.css
.card {
  width: 300px;
  border: 2px solid rgb(74, 125, 142);
  border-radius: 10px;
  font-size: 2em;
  padding: 0 20px;
  margin: 0 auto;
  background: lightblue;
  color: #333;
}

index.js
var user = {
  name: "王小明",
  birth: "2000-1-1",
};

observe(user); // 观察

// 显示姓氏
function showFirstName() {
  document.querySelector("#firstName").textContent = "姓:" + user.name[0];
}

// 显示名字
function showLastName() {
  document.querySelector("#lastName").textContent = "名:" + user.name.slice(1);
}

// 显示年龄
function showAge() {
  var birthday = new Date(user.birth);
  var today = new Date();
  today.setHours(0), today.setMinutes(0), today.setMilliseconds(0);
  thisYearBirthday = new Date(
    today.getFullYear(),
    birthday.getMonth(),
    birthday.getDate()
  );
  var age = today.getFullYear() - birthday.getFullYear();
  if (today.getTime() < thisYearBirthday.getTime()) {
    age--;
  }
  document.querySelector("#age").textContent = "年龄:" + age;
}

autorun(showFirstName);
autorun(showLastName);
autorun(showAge);


euv.js
/**
 * 观察某个对象的所有属性
 * @param {Object} obj
 */
function observe(obj) {
  for (const key in obj) {
    let internalValue = obj[key];
    let funcs = [];
    Object.defineProperty(obj, key, {
      get: function () {
        //  依赖收集,记录:是哪个函数在用我
        if (window.__func && !funcs.includes(window.__func)) {
          funcs.push(window.__func);
        }
        return internalValue;
      },
      set: function (val) {
        internalValue = val;
        // 派发更新,运行:执行用我的函数
        for (var i = 0; i < funcs.length; i++) {
          funcs[i]();
        }
      },
    });
  }
}

/**
* 执行autorun函数的时候先把目标函数fn添加到window的__func属性中
* 执行目标函数的时候,触发了get函数,将目标函数添加到内置数组funcs里面
* 当data中的变量进行赋值的时候,触发了set函数,运行内部数组funcs里面的目标函数fn
* 至此简易的数据响应式便实现了。
*/
function autorun(fn) {
  window.__func = fn;
  fn();
  window.__func = null;
}

标签:funcs,function,Vue,函数,js,window,func,脚手架,today
From: https://www.cnblogs.com/crispyChicken/p/17202919.html

相关文章

  • 在JSP页面对一组数据进行判断之后输出
    需求:在合同信息管理页面做一个临期提醒功能,即在终止日期前一个月将该条记录显示在别处以提醒工作人员。实现思路:我在建表时多加了一项bool型数据,用来标记这项数据是否在一......
  • Vue 插槽
    Vue插槽环境准备(就一个分类组件)###Category.vue<template> <divclass="category"> <h3>xxx分类</h3> <ul> <li>11111</li> <li>2222</li> <li>33......
  • 从three.js 开始
    02本地运行本地运行加https不能直接localhost1本地2parceljs(类似webpack等)新建文件夹npminitnpminstallparcel-bundler普通依赖--save-dev是设置为开......
  • java String转Json工具类
    importcom.fasterxml.jackson.core.JsonProcessingException;importcom.fasterxml.jackson.databind.ObjectMapper;importjava.util.HashMap;importjava.util.List;imp......
  • Vue3+vite+antDesign 日历 月份和星期中文显示
    <template><a-config-provider:locale="locale">//建议直接包裹在最外层,若果你想全部转换为中文的话</a-config-provider><template><scriptsetup......
  • vue3.2+ts 子组件的props接收的是个对象的话
    备忘录而已:<scriptsetuplang="ts">//import{PropType}from"vue"exportinterfaceDataInfo{id:number;name:string}typeProps={isObject?:DataInfo}//c......
  • # vue2 使用 cesium 【第二篇-相机视角移动+添加模型】
    vue2使用cesium【第二篇-相机视角移动+添加模型】搞了一阵子cesium,小白入门,这东西很牛逼,但是感觉这东西好费劲啊!网上资料不多,每个人的用法又不一样,操作起来真的是绝......
  • vue router中useRouter,userRoute的区别
    ......
  • vue 遍历的汉字显示不同的颜色
    <template><div><divclass="stars"><spanv-for="(star,index)instars":key="index":class="starClass(index)">......
  • VUE-select-option 获取 label value 值
    1.绑定的value值中多赋值一个参数:value="{value:item.value,label:item.label}"2.添加@change事件进行获取值<template><el-selectv-model="value"placehol......