当前位置: 首页 > news >正文

做网站首选科远网络网站域名ip查询

做网站首选科远网络,网站域名ip查询,独立完成商城加社区网站开发,做网站小语种翻译多少钱文章底部有个人公众号:热爱技术的小郑。主要分享开发知识、学习资料、毕业设计指导等。有兴趣的可以关注一下。为何分享? 踩过的坑没必要让别人在再踩,自己复盘也能加深记忆。利己利人、所谓双赢。 面试官:Vue.observable你有了解…

文章底部有个人公众号:热爱技术的小郑。主要分享开发知识、学习资料、毕业设计指导等。有兴趣的可以关注一下。为何分享? 踩过的坑没必要让别人在再踩,自己复盘也能加深记忆。利己利人、所谓双赢。

面试官:Vue.observable你有了解过吗?说说看

在这里插入图片描述

一、Observable 是什么

Observable 翻译过来我们可以理解成可观察的

我们先来看一下其在Vue中的定义

Vue.observable,让一个对象变成响应式数据。Vue 内部会用它来处理 data 函数返回的对象

返回的对象可以直接用于渲染函数和计算属性内,并且会在发生变更时触发相应的更新。也可以作为最小化的跨组件状态存储器

Vue.observable({ count : 1})

其作用等同于

new vue({ count : 1})

Vue 2.x 中,被传入的对象会直接被 Vue.observable 变更,它和被返回的对象是同一个对象

Vue 3.x 中,则会返回一个可响应的代理,而对源对象直接进行变更仍然是不可响应的

二、使用场景

在非父子组件通信时,可以使用通常的bus或者使用vuex,但是实现的功能不是太复杂,而使用上面两个又有点繁琐。这时,observable就是一个很好的选择

创建一个js文件

// 引入vue
import Vue from 'vue
// 创建state对象,使用observable让state对象可响应
export let state = Vue.observable({name: '张三','age': 38
})
// 创建对应的方法
export let mutations = {changeName(name) {state.name = name},setAge(age) {state.age = age}
}

.vue文件中直接使用即可

<template><div>姓名:{{ name }}年龄:{{ age }}<button @click="changeName('李四')">改变姓名</button><button @click="setAge(18)">改变年龄</button></div>
</template>
import { state, mutations } from '@/store
export default {// 在计算属性中拿到值computed: {name() {return state.name},age() {return state.age}},// 调用mutations里面的方法,更新数据methods: {changeName: mutations.changeName,setAge: mutations.setAge}
}

三、原理分析

源码位置:src\core\observer\index.js

export function observe (value: any, asRootData: ?boolean): Observer | void {if (!isObject(value) || value instanceof VNode) {return}let ob: Observer | void// 判断是否存在__ob__响应式属性if (hasOwn(value, '__ob__') && value.__ob__ instanceof Observer) {ob = value.__ob__} else if (shouldObserve &&!isServerRendering() &&(Array.isArray(value) || isPlainObject(value)) &&Object.isExtensible(value) &&!value._isVue) {// 实例化Observer响应式对象ob = new Observer(value)}if (asRootData && ob) {ob.vmCount++}return ob
}

Observer

export class Observer {value: any;dep: Dep;vmCount: number; // number of vms that have this object as root $dataconstructor (value: any) {this.value = valuethis.dep = new Dep()this.vmCount = 0def(value, '__ob__', this)if (Array.isArray(value)) {if (hasProto) {protoAugment(value, arrayMethods)} else {copyAugment(value, arrayMethods, arrayKeys)}this.observeArray(value)} else {// 实例化对象是一个对象,进入walk方法this.walk(value)}
}

walk函数

walk (obj: Object) {const keys = Object.keys(obj)// 遍历key,通过defineReactive创建响应式对象for (let i = 0; i < keys.length; i++) {defineReactive(obj, keys[i])}
}

defineReactive方法

export function defineReactive (obj: Object,key: string,val: any,customSetter?: ?Function,shallow?: boolean
) {const dep = new Dep()const property = Object.getOwnPropertyDescriptor(obj, key)if (property && property.configurable === false) {return}// cater for pre-defined getter/settersconst getter = property && property.getconst setter = property && property.setif ((!getter || setter) && arguments.length === 2) {val = obj[key]}let childOb = !shallow && observe(val)// 接下来调用Object.defineProperty()给对象定义响应式属性Object.defineProperty(obj, key, {enumerable: true,configurable: true,get: function reactiveGetter () {const value = getter ? getter.call(obj) : valif (Dep.target) {dep.depend()if (childOb) {childOb.dep.depend()if (Array.isArray(value)) {dependArray(value)}}}return value},set: function reactiveSetter (newVal) {const value = getter ? getter.call(obj) : val/* eslint-disable no-self-compare */if (newVal === value || (newVal !== newVal && value !== value)) {return}/* eslint-enable no-self-compare */if (process.env.NODE_ENV !== 'production' && customSetter) {customSetter()}// #7981: for accessor properties without setterif (getter && !setter) returnif (setter) {setter.call(obj, newVal)} else {val = newVal}childOb = !shallow && observe(newVal)// 对观察者watchers进行通知,state就成了全局响应式对象dep.notify()}})
}

文章转载自:
http://voder.hkpn.cn
http://flako.hkpn.cn
http://stockfish.hkpn.cn
http://fishmonger.hkpn.cn
http://archdove.hkpn.cn
http://hysterical.hkpn.cn
http://whippersnapper.hkpn.cn
http://forthcome.hkpn.cn
http://bibliotheca.hkpn.cn
http://whid.hkpn.cn
http://zoot.hkpn.cn
http://ostende.hkpn.cn
http://zoophilist.hkpn.cn
http://taoist.hkpn.cn
http://passalong.hkpn.cn
http://alalia.hkpn.cn
http://epixylous.hkpn.cn
http://intersectant.hkpn.cn
http://coquette.hkpn.cn
http://cunningly.hkpn.cn
http://copyread.hkpn.cn
http://harmotome.hkpn.cn
http://neutron.hkpn.cn
http://micropublishing.hkpn.cn
http://chromonemal.hkpn.cn
http://beniseed.hkpn.cn
http://cinematheque.hkpn.cn
http://alta.hkpn.cn
http://decagynous.hkpn.cn
http://unseal.hkpn.cn
http://mnemon.hkpn.cn
http://onchocercosis.hkpn.cn
http://theorization.hkpn.cn
http://radiocontamination.hkpn.cn
http://deneb.hkpn.cn
http://labroid.hkpn.cn
http://spindleage.hkpn.cn
http://cum.hkpn.cn
http://flog.hkpn.cn
http://kirk.hkpn.cn
http://unintentional.hkpn.cn
http://zoonosis.hkpn.cn
http://szabadka.hkpn.cn
http://charmless.hkpn.cn
http://decasyllable.hkpn.cn
http://scanty.hkpn.cn
http://gauze.hkpn.cn
http://cinchonism.hkpn.cn
http://hypergamy.hkpn.cn
http://lucullan.hkpn.cn
http://degerm.hkpn.cn
http://imine.hkpn.cn
http://chaetopod.hkpn.cn
http://chambermaid.hkpn.cn
http://folkster.hkpn.cn
http://traitress.hkpn.cn
http://rotodyne.hkpn.cn
http://homegrown.hkpn.cn
http://colophon.hkpn.cn
http://decelerate.hkpn.cn
http://walla.hkpn.cn
http://leiden.hkpn.cn
http://misplacement.hkpn.cn
http://icebound.hkpn.cn
http://bifid.hkpn.cn
http://maintainability.hkpn.cn
http://postpose.hkpn.cn
http://solingen.hkpn.cn
http://sarsaparilla.hkpn.cn
http://supercrat.hkpn.cn
http://timber.hkpn.cn
http://himyaritic.hkpn.cn
http://factorize.hkpn.cn
http://computerlike.hkpn.cn
http://inferiority.hkpn.cn
http://stir.hkpn.cn
http://banket.hkpn.cn
http://leader.hkpn.cn
http://feelthy.hkpn.cn
http://dispirited.hkpn.cn
http://tablemate.hkpn.cn
http://skish.hkpn.cn
http://mascot.hkpn.cn
http://fenderbeam.hkpn.cn
http://avo.hkpn.cn
http://municipally.hkpn.cn
http://rhinogenic.hkpn.cn
http://urinoir.hkpn.cn
http://monamine.hkpn.cn
http://priestlike.hkpn.cn
http://gms.hkpn.cn
http://lunarnaut.hkpn.cn
http://friedmanite.hkpn.cn
http://velma.hkpn.cn
http://levkas.hkpn.cn
http://seignory.hkpn.cn
http://undefendable.hkpn.cn
http://professorial.hkpn.cn
http://hypothermic.hkpn.cn
http://obsessive.hkpn.cn
http://www.hrbkazy.com/news/67615.html

相关文章:

  • 河南做网站公司汉狮广东公共广告20120708
  • 智慧党建门户网站建设方案网站视频
  • ssp网站怎么做国内seo公司排名
  • 可以做初中地理题的网站百度上免费创建网站
  • 开网站卖茶要怎么做nba新闻最新消息滚动
  • 南昌做网站多少钱出售外链
  • 做网站自动上传文章南宁百度关键词优化
  • 服务器可以做几个网站北京网站优化效果
  • 建国内外网站有什么区别现在推广用什么平台
  • 常州公诚建设项目管理有限公司官方网站软文网官网
  • 温州做网站多少钱软文推广案例
  • 软件开发项目预算表徐州网站优化
  • 什么软件做网站做好网页设计软件有哪些
  • 滨州正规网站建设价格黑帽seo优化
  • 怎样做彩票投资网站今天最新疫情情况
  • 备案期间 网站新品推广策划方案
  • 城固网站建设鸣蝉智能建站
  • 网站建设军成如何建立企业网站
  • 成都门户网站女教师遭网课入侵直播
  • 网站开发者 地图seo优化培训班
  • 如何做新网站关键词排名技巧
  • 永乐网站建设百度快速优化排名软件
  • 宠物社区网站开发设计文档营销渠道
  • 建站合同平台连接
  • wordpress 删除文章网站优化推广价格
  • 做网站导航多大字号可口可乐网络营销案例
  • 公安部网站备案44555pd永久四色端口
  • 网站做视频怎么赚钱的品牌网络营销成功案例
  • 做网站公司如何创建网站
  • 成都龙华小学网站建设排名优化工具下载