本篇内容主要讲解“vue mixins代码如何复用”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“vue mixins代码如何复用”吧!
<!-- 主文件 -->
<template>
<button @click="clickHandle">button</button>
</template>
<script>
export default {
name: 'PageDemo',
methods: {
func1(){},
func2(){},
func3(){},
clickHandle(){
this.func1();
this.func2()
this.func3()
console.log('button clicked')
}
},
}
</script>
如果当前组件不好拆分,就会出现很多函数,代码会显得不清晰。 我现在的处理方法是通过mixins
混入,参照MVC思想,当前文件的的methods只写和模板直接引用的处理方法,其他的函数都通过混入方式引用。
// compose-demo.js
export default {
methods: {
func1(){},
func2(){},
func3(){},
}
}
<template>
<button @click="clickHandle">button</button>
</template>
<script>
// 主文件
import ComposeDemo from './compose-demo'
export default {
name: 'PageDemo',
mixins: [ComposeDemo],
methods: {
clickHandle(){
this.func1();
this.func2()
this.func3()
console.log('button clicked')
}
},
}
</script>
充分利用mixins
还有很多优点。
v-model
不适用。需要采用$emit
方法// 组件
<template>
<input v-model="a" @change="inputChangeHandle"/>
<input v-model="b" @change="inputChangeHandle" />
</template>
<script>
export default {
name: 'ComponentChild',
props: {
propA: {
type: String
},
propB: {
type: String
}
},
data(){
return {
a: this.propA,
b: this.propB,
}
},
methods: {
inputChangeHandle(){
this.$emit('update-data', {a:this.a, b:this.b})
}
}
}
</script>
// 调用方
<template>
<component-child :propA="query.a" :propB="query.b" @update-data="getData"/>
</template>
<script>
import ComponentChild from './component-child.vue'
export default {
name: 'Page1',
components: {ComponentChild},
data(){
return {
query: {
a: '默认数据a',
b: '默认数据b'
}
}
},
methods: {
getData(payload) {
const {a,b}=payload;
this.query.a = a;
this.query.b = b;
}
}
}
</script>
如果你有多处地方需要用到ComponentChild
组件,那每个调用地方都需要写一个方法来监听@update-data
事件。
此时,可以这样改一下
// 纯函数,引入ComponentChild,并且声明getData方法
// compose-component-child.js
<script>
import ComponentChild from './component-child.vue'
</script>
export default {
components: {ComponentChild},
methods: {
// 通常情况,复用的业务组件都会有同样的数据结构,都带有query.a和query.b。如果不一致,那直接在父组件重写该方法
getData(payload) {
const {a,b}=payload;
this.query.a = a;
this.query.b = b;
}
}
}
// 调用方
<template>
<component-child :propA="query.a" :propB="query.b" @update-data="getData"/>
</template>
<script>
import ComposeComponentChild from './compose-component-child.js'
export default {
name: 'Page1',
mixins: [ComposeComponentChild]
data(){
return {
query: {
a: '默认数据a',
b: '默认数据b'
}
}
},
methods: { }
}
</script>
借鉴了Angular的依赖注入,Page
不直接声明、引用Component
,而是通过混入Compose
直接使用。
Component
组件,Compose
引入Component
并且注册Component
(声明额外的方法),Page
调用组件混入Compose
,就可以可以直接使用Component
组件
比如常用的表格需要一下数据
<script>
import {defaultPageSize}from '@/setting'
export default {
data(){
return {
tableList: [],
pageSize: defaultPageSize,
pageNo: 1,
totalRecords: 0,
}
}
}
</script>
以上数据都可以组装为一个compose-table.js
文件混入到你要使用的地方,当然也可以通过在compose-table
引用注册表格组件。
到此,相信大家对“vue mixins代码如何复用”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。