设计模式在vue中的应用

外观模式、工厂模式在vue中应用

formCom文件

<template>
    <div>
        <form>
            <login
                v-for='item of comInp'
                :name = 'item'
                :key='item'
            ></login>
        </form>
    </div>
</template>
<script>
import login from './login';
const inputList = {
    login: ['Username', 'Password', 'ImgCode'],
    mobile: ['mobile', 'Password']
}
export default {
    name: 'Form',
    data() {
        return {
            type: 'login'
        }
    },
    components: {
        login
    },
    computed: {
        comInp(){
            console.log(inputList)
            return inputList[this.type]
        }
    },
    
}
</script>

login文件

<template>
    <component :is='comInput' />
</template>
<script>
import Username from './input/Username'
import Password from './input/Password'
import ImgCode from './input/ImgCode'
import Mobile from './input/mobile'

const inpMap = {
    Username,
    Password,
    ImgCode,
    Mobile
}
export default {
    name: 'login',
    data() {
        return {
            codeStatus: 'img'
        }
    },
    components: inpMap,
    props: ['name'],
    computed: {
        comInput(){
            return inpMap[this.name]
        }
    },
}
</script>
<style scope>
    
</style>

状态模式在vue中应用

stepHome文件

<template>
    <div>
        <div>
            <div>步骤一<span>{{state > 1 && '完成' || '未完成'}}</span></div>
            <div>步骤二<span>{{state > 2 && '完成' || '未完成'}}</span></div>
            <div>步骤三<span>{{state > 3 && '完成' || '未完成'}}</span></div>
            <div>步骤四<span>{{state > 4 && '完成' || '未完成'}}</span></div>
        </div>
        <button v-if="canGoBack" @click="goBack">返回上一步</button>
        <component :is="stepName"  @getChildData = 'changeState' />
    
    </div>
    

</template>

<script>
import step1 from './step/step1'
import step2 from './step/step2'
import step3 from './step/step3'
import step4 from './step/step4'
export default {
    name: 'stepHome',
    data() {
        return {
            state: 1,
            cache: []
        }
    },
    computed: {
        stepName(){
            const stepList = {
                1: step1,
                2: step2,
                3: step3,
                4: step4,
            }
            return stepList[this.state]
        },
        canGoBack(){
            return this.cache.length > 0
        }
    },
    methods: {
        changeState(state){
            this.cache.push(state)
            this.$set(this, 'state', state)
        },
        goBack(){
            this.cache.pop()
            this.state = this.cache[this.cache.length - 1]
        }
    }, 
}
</script>

<style>
    span{
        margin-left: 10px;
        color: red;
    }
</style>

step1文件

<template>
    <div>
        <button @click='finshOne'>点击完成步骤一</button>
    </div>
</template>
<script>
export default {
    methods: {
        finshOne(){
            this.$emit('getChildData', 2)
        }
    },
}
</script>

step2文件

<template>
    <div>
        <button @click='finshOne'>点击完成步骤二</button>
    </div>
</template>
<script>
export default {
    methods: {
        finshOne(){
            this.$emit('getChildData', 3)
        }
    },
}
</script>

step3文件

<template>
    <div>
        <button @click='finshOne'>点击完成步骤三</button>
    </div>
</template>
<script>
export default {
    methods: {
        finshOne(){
            this.$emit('getChildData', 4)
        }
    },
}
</script>

step4文件

<template>
    <div>
        <button @click='finshOne'>点击完成步骤四</button>
    </div>
</template>
<script>
export default {
    methods: {
        finshOne(){
            this.$emit('getChildData', 5)
        }
    },
}
</script>
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 本文首发于个人博客:Lam's Blog - 谈谈23种设计模式在Android源码及项目中的应用,文章由Mark...
    格子林ll阅读 4,759评论 1 105
  • 基于Vue的一些资料 内容 UI组件 开发框架 实用库 服务端 辅助工具 应用实例 Demo示例 element★...
    尝了又尝阅读 1,258评论 0 1
  • 开发一个项目,采用什么语言都可以,主要能熟练高效的开发都是合理的,这次我们采用vue来开发一个团队项目。在开...
    MsgSS阅读 3,049评论 3 9
  • 来源:http://www.cnblogs.com/opendigg/p/6513510.html UI组件 el...
    YU_XI阅读 2,932评论 0 25
  • 目录 上一篇 寒冰听见雪菲儿的叫声,不顾黑洞危险进入其中,却遭受了攻击,而攻击她的人,竟然是雪菲儿! 雪菲儿双目血...
    刘白月阅读 402评论 0 1

友情链接更多精彩内容