当前位置: 首页 > 科技观察

【译】Vue2.0变化(一)基础API变化

时间:2023-03-17 16:09:18 科技观察

高阶变化模板解析器不再依赖DOM(除非你使用真正的DOM作为模板),所以只要你使用字符串模板,你就不会不再受1.0版中的任何解析限制。但是,如果您依赖于在现有内容中安装一个元素作为模板(使用el元素),您仍然会受到这些限制。编译器(将字符串模板转换为渲染方法的部分)和运行时现在可以分开。有两种不同的构建:独立构建:包括编译和运行。这种方式和vue1.0几乎一模一样。运行时编译:由于它不包含编译器,因此模板要么在编译时预编译,要么手动编写渲染函数。npm包默认导出这个版本,所以你需要有一个编译过程(使用Browserify或Webpack),从中vueify或vue-loader将能够预编译模板。全局配置Vue.config.silentVue.config.optionMergeStrategiesVue.config.devtoolsVue.config.errorHandler(新API,全局钩子用于处理组件渲染和监控时未捕获的错误)Vue.config.keyCodes(新API,别名v-on配置的自定义密钥)Vue.config.debug(废弃)Vue.config.async(废弃)Vue.config.delimiters(废弃)Vue.config.unsafeDelimiters(废弃,使用v-html)全局APIVue。extendVue.nextTickVue.setVue.deleteVue.directiveVue.componentVue.useVue.mixinVue.compile(newAPI,onlyforstandalonebuilds)Vue.transitionstagger(废弃,在elVue.elementDirective上设置Vue.filter(废弃,使用组件)Vue.partial(废弃,使用功能组件)optionsdatadataprops.prop.default.coerce(废弃,如果需要转换props,请使用compute属性)。prop绑定模式(废弃,v-model适用于组件propsData(新API)只能用于实例计算方法watchDOMel模板渲染(新API)替换(废弃,组件现在必须有根元素)lifecycleHooksinit(废弃,请使用beforeCreate)createdbeforeDestroydestroyedbeforeMount(newAPI)mounted(newAPI)beforeUpdate(newAPI)updated(newAPI)activated(newAPI,forkeep-alive)deactivated(newAPIforkeep-alive)ready(discarded),usemounted)activate(discarded,migratetovue-router)beforeCompile(discarded,usecreated)compiled(discarded,usemounted)attached(discarded)detached(discarded,同上)Assetsdirectivescomponentstransitionsfilterspartials(discarded)elementDirectives(废弃)miscellaneousparentmixinsnameextendsdelimiters(newAPI,replacetheoriginalglobalconfigurationoptions,onlyavailableinstandalonebuilds)functional(newAPI)events(discarded)实例方法数据vm.$watchvm.$get(丢弃,直接取值)vm.$set(丢弃,使用Vue.set)vm.$delete(丢弃,使用Vue.delete)vm.$eval(丢弃,无实际用处)vm.$interpolate(丢弃,同上)vm.$log(丢弃,使用devtools)eventsvm.$onvm.$oncevm.$offvm.$emitvm.$dispatch(废弃,使用全局事件或者使用vuex,见下文)vm.$broadcast(废弃,同上)DOMvm.$nextTickvm.$appendTo(废弃,使用nativevm.$el)vm.$before(discarded)vm.$after(discarded)vm.$remove(discarded)生命周期vm.$mountvm.$destroy指令v-textv-html(注意{{{}}}被丢弃)v-ifv-showv-elsev-forkey(替换跟踪)。对象v-for。范围v-for。更新参数顺序:数组中使用arr中的(value,index),对象中使用obj中的(value,key,index)。$index和$key被v-on丢弃。修饰符。在子组件上。自定义键码,当前版本Vue.config.keyCodes替换了原来的Vue.directive('on').keyCodesv-bind。作为道具。x链接。绑定对象v-bind:styleprefixsniffingv-bind:classv-modellazy(asmodifier)number(asmodifier)ignoringcompositioneventsdebounce(discarded,usev-on:input)v-cloakv-prev-once(新API)v-ref(废弃,现在只是一个特殊属性ref)v-el(与ref合并)特殊组件:isasynccomponentinline-templatepartial(discarded)特殊属性keyrefslot服务器端渲染renderToStringrenderToStream客户端水合