vue-learning:21 - js - mixins
mixins
混入是一种对重复代码的组织方式,可以在多个组件间复用代码。
如果在项目中,在多个组件间有一段逻辑代码是共同的。那常见的处理方式是:
- 每个组件都复制粘贴代码(显然这是最不好方式)
- 将以共同的代码逻辑抽离成多个函数,并存储到util文件里,在组件使用时引入这个方法。
- 直接将共同的代码剥离出来,并存储到你定义的mixin文件里,在组件使用时引入,并在实例选项中全局混入,或组件中局部混入。
后两种方式在思想上是一致的,但是mixin混入比方法抽离更为彻底,在混入中完全按照vue的代码组织方式,可以使用method或生命周期钩子函数,可以包含this对象等。而抽离成工具方法中,不能使用vue相关的选项属性,只能是将重复的逻辑代码自定义成函数形式。
混入对象可以引用任何`vue`组件所能引用的东西,就好像它是组件本身一样
基本使用:
比如在项目中有几个组件都要获取用户信息,调用getUserInfo方法。则可以按下面这么操作:
// userInfoMixin.js
export const userInfoMixin = {
methods: {
getUserInfo() {
return fetch(`api/user/${this.userId}`)
.then(res => res.json())
}
}
}
// A组件内,先引入,再使用
import {userInfoMixin} from '.mixins/userInfoMixin'
export default {
mixins: [userInfoMixin],
props: {
userId: {
type: number
}
}
data() {
return {
userInfo: null,
}
},
mounted() {
// 直接调用this打点调用混入的方法,就像使用自身定义的一样
this.getUserInfo()
.then(res => {
this.userInfo = res
})
}
}
但上面这个例子,我们只虽剥离一个共同的方法。在utils中经常类似这么操作。实际上混入可以使用vue组件的任意属性,所以上面的data中的userInfo和mounted中的方法调用代码都是重复的,所以可以按下面优化后,这样混入:
// userInfoMixin.js
export const userInfoMixin = {
data() {
return {
userInfo: null,
}
},
mounted() {
this.getUserInfo()
.then(res => {
this.userInfo = res
})
}
methods: {
getUserInfo() {
return fetch(`api/user/${this.userId}`)
.then(res => res.json())
}
}
}
// A组件内,先引入,再使用
import {userInfoMixin} from '.mixins/userInfoMixin'
export default {
mixins: [userInfoMixin],
props: {
userId: {
type: number
}
}
}
虽然混入使组件简化了很多,但是让追踪数据的来源变得复杂了。当决定将哪些代码放在混入对象中,哪些代码保留在组件中,你必须衡量这样做的代价和收益。
混入注意事项
如果混入对象和组件间有重复的选项,vue会根据重复的选项类型来区别对待
- 对生命周期钩子函数的重复,会将它们添加到一个数组中,并会全部执行。
- 对其它重复的数据、方法、计算属性等非生命周期函数,组件中的属性优先级更高,会覆盖混入的对应项。
//mixin
export const hi = {
mounted() {
console.log('hello from mixin!')
}
}
//vue instance or component
new Vue({
el: '#app',
mixins: [hi],
mounted() {
console.log('hello from Vue instance!')
}
});
//当组件创建时,`mixin`混入中信息会先于组件中信息的输出。
> hello from mixin!
> hello from Vue instance!
//mixin
export const hi = {
methods: {
sayHello: function() {
console.log('hello from mixin!')
}
},
mounted() {
this.sayHello()
}
}
//vue instance or component
new Vue({
el: '#app',
mixins: [hi],
methods: {
sayHello: function() {
console.log('hello from Vue instance!')
}
},
mounted() {
this.sayHello()
}
})
// 组件内的方法覆盖了`mixin`的同名方法。
> hello from Vue instance!
> hello from Vue instance!
混入内对象的命名规范
有时候会特意用到这种冲突的覆盖,但有时需要避免这种冲突覆盖,在vue官方代码风格指南中建议对于混入的私有属性(不应该在在混入之外在使用的方法、数据或计算属性),应该在它们名称前面添加前缀$_以示区分。比如上面会被覆盖的方法可以命名成$_sayHello。这在开发自定义组件或插件时极为重要,因为用户在使用插件的项目中,可能会使用相同的属性名称,如果重名会造成BUG。
混入 mixin 的源码解读
其实源码中mixin原理并不复杂,只是将mixin中的对象与vue自身的options属性合并在一起。复杂是需要判断混入对象中的各个属性,根据属性不同采用不用的合并策略。
// 文件位置: vue/src/core/index
// vue实例初始化全局API
import { initGlobalAPI } from './global-api/index'
initGlobalAPI(Vue)
// 文件位置: vue/src/core/goloba-api/index
import { initMixin } from './mixin'
initMixin(Vue)
// 文件位置:vue/src/core/global-api/mixin
import { mergeOptions } from '../util/index'
export function initMixin (Vue) {
Vue.mixin = function (mixin: Object) {
this.options = mergeOptions(this.options, mixin)
return this
}
}
// mergeOptions方法在:vue/src/core/util/options
export function mergeOptions () {
// ...其它代码
const options = {}
let key
for (key in parent) {
mergeField(key)
}
for (key in child) {
// 如果组件parent内已经处理过的属性,则不再处理
if (!hasOwn(parent, key)) {
mergeField(key)
}
}
function mergeField (key) {
const strat = strats[key] || defaultStrat
options[key] = strat(parent[key], child[key], vm, key)
}
return options
}
// 不同的全并策略
strats.data = function (){ /* do something */}
strats.watch = function (){ /* do something */}
strats.props =
strats.methods =
strats.inject =
strats.computed = function (){ /* do something */}
// 对于component / directive / filter 等模板依赖相关的属性的混入策略
ASSET_TYPES.forEach(function (type) {
strats[type + 's'] = mergeAssets
})
// 对于生命周期钩子的混入策略
LIFECYCLE_HOOKS.forEach(hook => {
strats[hook] = mergeHook
})
/**
* Hooks are merged as arrays.
*/
function mergeHook (
parentVal: ?Array<Function>,
childVal: ?Function | ?Array<Function>
): ?Array<Function> {
const res = childVal
? parentVal
? parentVal.concat(childVal)
: Array.isArray(childVal)
? childVal
: [childVal]
: parentVal
return res
? dedupeHooks(res)
: res
}
function dedupeHooks (hooks) {
const res = []
for (let i = 0; i < hooks.length; i++) {
if (res.indexOf(hooks[i]) === -1) {
res.push(hooks[i]) // 推入数组中
}
}
return res
}
vue-learning:21 - js - mixins的更多相关文章
- 从壹开始前后端分离 [ Vue2.0+.NET Core2.1] 十五 ║Vue基础:JS面向对象&字面量& this字
缘起 书接上文<从壹开始前后端分离 [ Vue2.0+.NET Core2.1] 十四 ║ VUE 计划书 & 我的前后端开发简史>,昨天咱们说到了以我的经历说明的web开发经历的 ...
- vue.js mixins 使用
export default { data () { return { } }, created () { }, methods: { arrayContain (array, obj) { for ...
- vue中config/index.js:配置的详细理解
当我们需要和后台分离部署的时候,必须配置config/index.js: 用vue-cli 自动构建的目录里面 (环境变量及其基本变量的配置) var path = require('path') ...
- iosselect:一个js picker项目,在H5中实现IOS的select下拉框效果
具体文档和demo可以访问github:https://github.com/zhoushengmufc/iosselect 移动端浏览器对于select的展示样式是不一致的,ios下是类似原生的pi ...
- Deep learning:五十一(CNN的反向求导及练习)
前言: CNN作为DL中最成功的模型之一,有必要对其更进一步研究它.虽然在前面的博文Stacked CNN简单介绍中有大概介绍过CNN的使用,不过那是有个前提的:CNN中的参数必须已提前学习好.而本文 ...
- iosselect:一个js picker项目,在H5中实现IOS的下拉效果
iosselect是在webapp下的一个picker组件,可以轻松实现各类选择器效果.比如地区选择 时间选择 日期选择等. 下面是一个地址选择器demo截图,可以访问:http://zhoushen ...
- Atom 编辑器安装 linter-eslint 插件,并配置使其支持 vue 文件中的 js 格式校验
安装方式有如下几种. 1.最常用的安装方式. # 进入atom插件文件夹 cd ~/.atom/packages/ # git clone 插件源文件 git clone https://github ...
- 从壹开始前后端分离 [ Vue2.0+.NET Core2.1] 二十二║Vue实战:个人博客第一版(axios+router)
前言 今天正式开始写代码了,之前铺垫了很多了,包括 6 篇基础文章,一篇正式环境搭建,就是为了今天做准备,想温习的小伙伴可以再看看<Vue 基础入门+详细的环境搭建>,内容很多,这里就暂时 ...
- 从壹开始前后端分离 [ Vue2.0+.NET Core2.1] 二十三║Vue实战:Vuex 其实很简单
前言 哈喽大家周五好,马上又是一个周末了,下周就是中秋了,下下周就是国庆啦,这里先祝福大家一个比一个假日嗨皮啦~~转眼我们的专题已经写了第 23 篇了,好几次都坚持不下去想要中断,不过每当看到群里的交 ...
随机推荐
- “获取access_token”接口新增IP白名单保护
- 源码篇:MBProgressHUD
源码来源:点此 版本:0.9.1 MBProgressHUD是一个显示HUD窗口的第三方类库,用于在执行一些后台任务时,在程序中显示一个表示进度的loading视图和两个可选的文本提示的HUD窗口.我 ...
- 阿里云oss上传图片报错,The OSS Access Key Id you provided does not exist in our records.解决方法
vue项目 1.安装OSS的Node SDK npm install ali-oss --save 2.参考官方提示https://help.aliyun.com/document_detail/11 ...
- ros自定义消息
ros自定义消息可以根据自身项目需求定义和封装想要的数据类型和数据结构.具体可以参考维基百科关于ros自定义消息部分 这里我只是记录自定义消息的要点部分: 1.首先要在工作空间下功能包中创建一个msg ...
- @codeforces - 1086F@ Forest Fires
目录 @description@ @solution@ @accepted code@ @details@ @description@ 一个无穷大的方格图,每个方格内都种了棵树. 一开始点燃了 n 棵 ...
- LeetCode80 Remove Duplicates from Sorted Array II
题目: Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? (Mediu ...
- Python语言的缺点
- Libev源码分析02:Libev中的IO监视器
一:代码流程 在Libev中,启动一个IO监视器,等待该监视器上的事件触发,然后调用该监视器的回调函数.整个的流程是这样的: 首先调用ev_default_loop初始化struct ev_loop ...
- @loj - 2339@ 「WC2018」通道
目录 @desription@ @solution@ @accepted code@ @details@ @desription@ 11328 年,C 国的科学家们研发了一种高速传送通道,可以在很短的 ...
- 2019-6-23-修复-dotnet-Core-缺SDK编译失败
title author date CreateTime categories 修复 dotnet Core 缺SDK编译失败 lindexi 2019-6-23 10:55:9 +0800 2019 ...