1. 效果演示

2. 主要知识点

  • 使用slot分发内容
  • 动态组件
  • 组件通信
  • 实例的生命周期
  • 表单

3. 遇到的问题

  • bus 通信 第一次 $on 监听不到

    // 解决bus 第一次通信 $on 监听不到
    this.$nextTick(function () {
    if (_this.totalSize - 1 == _this.order) {
    bus.$emit('submiteDisabled', disabledStatus)
    } else {
    bus.$emit('nextStepDisabled', disabledStatus)
    }
    })
  • bus 通信 $on 多次触发

    //  bus传值之后要进行销毁, 尤其是跳转页面进行使用的时候
    beforeDestroy() {
    if (this.totalSize - 1 == this.order) {
    bus.$off('submiteDisabled')
    } else {
    bus.$off('nextStepDisabled')
    }
    },

4. 代码整理

  • HTML

    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="UTF-8">
    <title>答题卡</title>
    <link rel="stylesheet" href="./css/style.css">
    </head>
    <body>
    <div id="app">
    <main-component v-for="(question, index) in questions" :key="index" :title="question.title" :order="index"
    v-if="index === currentOrder" :content="question.chooses">
    <template slot="title" slot-scope="props">
    <span>{{props.order}}. {{props.title}}</span>
    </template>
    <template slot="content" slot-scope="props">
    <component :is="props.type"
    :datas="question.chooses.values"
    :selected = "question.chooses.selected"
    :order = "index"
    :total-size = "questions.length"
    v-if="props.type != ''"></component>
    <textarea-component :value="question.chooses.value"
    :order="index"
    :total-size = "questions.length" v-else></textarea-component>
    </template>
    <template slot="functionalDomain">
    <button-component :now-order = "currentOrder"
    :total-elements = "questions.length"
    :next-step-disabled = "true"
    ></button-component>
    </template>
    </main-component>
    </div>
    <script src="./js/vue.js"></script>
    <script src="./js/index.js"></script>
    </body>
    </html>
  • JS

    var bus = new Vue();
    Vue.component('main-component', {
    props: {
    order: Number,
    title: String,
    content: {
    type: Object
    }
    },
    template: '<div class="main">\
    <div class="title">\
    <slot name="title" :title="mytitle" :order="myorder + 1"></slot>\
    </div>\
    <div class="content">\
    <slot name="content" :type="typeSelect"></slot>\
    </div>\
    <div class="footer">\
    <slot name="functionalDomain" ></slot>\
    </div>\
    </div>',
    data: function () {
    return {
    mytitle: this.title,
    myorder: this.order
    }
    },
    computed: {
    typeSelect: function () {
    let type_co = this.content.type;
    if ('radio' == type_co || 'checkbox' == type_co) {
    return type_co + '-component';
    }else{
    return '';
    }
    }
    },
    });
    Vue.component('radio-component', {
    props:{
    selected: String,
    datas:Array,
    order: Number,
    totalSize: Number },
    template: '<div>\
    <label v-for="(item, index) in mydatas" :key="index"><input type="radio" :value="item.value" v-model="results" />{{item.name}}</label>\
    </div>',
    data:function (){
    return {
    results: this.selected,
    mydatas: this.datas
    }
    },
    methods: {
    updatNextStepStatus: function (val) { let disabledStatus = true;
    let _this = this;
    if (val != '') {
    disabledStatus = false;
    }
    // 解决bus 第一次通信 $on 监听不到
    this.$nextTick(function () {
    if (_this.totalSize - 1 == _this.order) {
    bus.$emit('submiteDisabled', disabledStatus)
    } else {
    bus.$emit('nextStepDisabled', disabledStatus)
    } }) }
    },
    watch: {
    results: function (val) {
    this.updatNextStepStatus(val);
    bus.$emit('valueChange', this.order, val)
    },
    selected: function (val) {
    this.results = val
    }
    },
    mounted() {
    this.updatNextStepStatus(this.results);
    },
    beforeDestroy() {
    if (this.totalSize - 1 == this.order) {
    bus.$off('submiteDisabled')
    } else {
    bus.$off('nextStepDisabled')
    }
    },
    });
    Vue.component('checkbox-component', {
    props: {
    selected: Array,
    datas: Array,
    order: Number,
    totalSize: Number
    },
    template: '<div>\
    <label v-for="(item, index) in mydatas" :key="index"><input type="checkbox" :value="item.value" v-model="results" />{{item.name}}</label>\
    </div>',
    data: function () {
    return {
    results: this.selected,
    mydatas: this.datas
    }
    },
    methods: {
    updatNextStepStatus: function (newValue, oldValue) {
    let disabledStatus = true;
    let _this = this;
    if (oldValue.length < 2 && newValue.length < 2) {
    return;
    } else if (oldValue.length >= 2 && newValue.length < 2) {
    disabledStatus = true;
    } else if (newValue.length >= 3){
    alert("多选,最多选择3个选项")
    }else{
    disabledStatus = false;
    }
    // 解决bus 第一次通信 $on 监听不到
    this.$nextTick(function () {
    if (_this.totalSize - 1 == _this.order) {
    bus.$emit('submiteDisabled', disabledStatus)
    } else {
    bus.$emit('nextStepDisabled', disabledStatus)
    } })
    }
    },
    watch: {
    results: function (newValue, oldValue) {
    this.updatNextStepStatus(newValue, oldValue);
    bus.$emit('valueChange', this.order, newValue)
    },
    selected: function (val) {
    this.results = val
    }
    },
    mounted() {
    this.updatNextStepStatus(this.results,[]);
    },
    beforeDestroy() {
    if (this.totalSize - 1 == this.order) {
    bus.$off('submiteDisabled')
    } else {
    bus.$off('nextStepDisabled')
    }
    },
    });
    Vue.component('textarea-component', {
    props: {
    value: String,
    order: Number,
    totalSize: Number
    },
    template: '<textarea v-model="results" placeholder="不少于100字"></textarea>',
    data: function () {
    return {
    results: this.value,
    }
    },
    methods: {
    updatNextStepStatus: function (newValue, oldValue) { let disabledStatus = true;
    let _this = this;
    if (newValue.length > 0 && newValue.length <= 5) {
    disabledStatus = false;
    }else{
    disabledStatus = true;
    }
    // 解决bus 第一次通信 $on 监听不到
    this.$nextTick(function () {
    if (_this.totalSize - 1 == _this.order) {
    bus.$emit('submiteDisabled', disabledStatus)
    }else{
    bus.$emit('nextStepDisabled', disabledStatus)
    } }) }
    },
    watch: {
    results: function (newValue, oldValue) {
    this.updatNextStepStatus(newValue, '');
    bus.$emit('valueChange', this.order, newValue)
    },
    value: function (val) {
    this.results = val
    }
    },
    mounted() {
    this.updatNextStepStatus(this.results, '');
    },
    beforeDestroy() {
    if (this.totalSize - 1 == this.order) {
    bus.$off('submiteDisabled')
    }else{
    bus.$off('nextStepDisabled')
    } },
    });
    Vue.component('button-component', {
    props: {
    basicClasses: {
    type: Object,
    default: function () {
    return {
    'button-white': true
    }
    }
    },
    nextStepClasses: {
    type: Object,
    default: function () {
    return {
    'button-primary': true
    }
    }
    },
    nextStepDisabled:{
    type: Boolean,
    default: false
    },
    nowOrder: Number,
    totalElements: Number
    },
    template: '<div>\
    <button class="btn" :disabled="nextStepStatus" :class="primary" \
    v-if="order != total - 1" @click="handleNextStep">下一步</button>\
    <button class="btn" :disabled="submiteStatus" :class="primary" \
    v-if="order == total - 1" @click="handleSubmit">提交</button>\
    <button class="btn" :disabled="false" :class="basic"\
    v-if= "order != 0" @click="handleBackStep">上一步</button>\
    <button class="btn" :disabled="false" @click="handleReset" :class="basic">重置</button>\
    </div>',
    data() {
    return {
    basic: this.basicClasses,
    primary: this.nextStepClasses,
    nextStepStatus: this.nextStepDisabled,
    order: this.nowOrder,
    total: this.totalElements,
    submiteStatus: true
    }
    },
    methods: {
    handleNextStep: function () {
    bus.$emit('nextStep')
    },
    handleBackStep: function () {
    bus.$emit('backStep')
    },
    handleSubmit: function () {
    bus.$emit('submit')
    },
    handleReset: function () {
    let _this = this;
    bus.$emit('reset', _this.order)
    }
    },
    mounted() {
    let _this = this;
    bus.$on('nextStepDisabled', function (status) {
    _this.nextStepStatus = status });
    bus.$on('submiteDisabled', function (status) {
    _this.submiteStatus = status })
    }, });
    var app = new Vue({
    el: "#app",
    data: {
    currentOrder: 0,
    questions: [
    {
    title: '请问您的性别是?',
    chooses: {
    type: 'radio',
    values: [{ 'name': '男', 'value': '1' }, { 'name': '女', 'value': '2' }, { 'name': '保密', 'value': '3' }],
    selected: ''
    }
    },
    {
    title: '请选择您的兴趣爱好?',
    chooses: {
    type: 'checkbox',
    values: [{ 'name': '看书', 'value': 'read book' },
    { 'name': '游泳', 'value': 'swim' },
    { 'name': '跑步', 'value': 'run' },
    { 'name': '看电影', 'value': 'see movie' }],
    selected: []
    }
    },
    {
    title: '请介绍一下你自己',
    chooses: {
    type: 'text',
    value: ''
    }
    }, ]
    },
    methods: {
    handleNext: function () {
    let current = this.currentOrder;
    if (++current >= this.questions.length) {
    return;
    }else{
    this.currentOrder += 1;
    } },
    handleBack: function () {
    let currentOrder = this.currentOrder;
    if (-- currentOrder < 0) {
    return;
    }else{
    this.currentOrder -= 1;
    }
    },
    handleReset: function (order, val) {
    let currentQuestion = this.questions[order].chooses;
    if (currentQuestion.type == 'radio') {
    currentQuestion.selected = val === undefined ? '': val;
    } else if (currentQuestion.type == 'checkbox') {
    currentQuestion.selected = val === undefined ? [] : val;
    } else {
    currentQuestion.value = val === undefined ? '' : val
    }
    }
    },
    mounted: function () {
    let _this = this;
    bus.$on('nextStep', function (msg) {
    let current = _this.currentOrder;
    if (++current >= _this.questions.length) {
    return;
    } else {
    _this.currentOrder += 1;
    }
    });
    bus.$on('backStep', function (msg) {
    let currentOrder = _this.currentOrder;
    if (--currentOrder < 0) {
    return;
    } else {
    _this.currentOrder -= 1;
    }
    });
    bus.$on('submit', function (msg) {
    console.log('提交')
    });
    bus.$on('reset', function (order) {
    _this.handleReset(order)
    });
    bus.$on('valueChange', function (order, val) {
    _this.handleReset(order, val)
    });
    }
    })
  • CSS

    [v-cloak]{
    display: none;
    }
    .btn {
    border: none;
    outline:none;
    color: white;
    padding: 7px 25px;
    text-align: center;
    text-decoration: none;
    display: inline-block;
    font-size: 16px;
    margin: 4px 2px;
    cursor: pointer;
    }
    .button-white {
    background-color: rgb(217, 219, 223);
    color: #000000;
    }
    .button-white:active {
    background-color: rgb(134, 149, 179);
    color: #000000;
    }
    .button-primary {
    background-color: rgb(39, 126, 228);
    border: none;
    color: white;
    padding: 7px 25px;
    text-align: center;
    text-decoration: none;
    display: inline-block;
    font-size: 16px;
    margin: 4px 2px;
    cursor: pointer;
    }
    .button-primary:active {
    background-color: rgb(185, 171, 31);
    }
    .button-primary:disabled {
    background-color: rgb(115, 122, 130);
    }

5. 升级

  • 组件重复内容很多,可合并

调查问卷WebApp的更多相关文章

  1. "琳琅满屋"调查问卷 心得体会及结果分析

    ·关于心得体会       当时小组提出这个校园二手交易市场的时候,就确定了对象范围,仅仅是面向在校大学生,而且在我们之前就已经有了很多成功的商品交易的例子可以让我们去借鉴,再加上我们或多或少的有过网 ...

  2. JavasScript实现调查问卷插件

    原文:JavasScript实现调查问卷插件 鄙人屌丝程序猿一枚,闲来无事,想尝试攻城师是感觉,于是乎搞了点小玩意.用js实现调查问卷,实现了常规的题型,单选,多选,排序,填空,矩阵等. 遂开源贴出来 ...

  3. 关于“Durian”调查问卷的心得体会

    这周我们做了项目着手前的客户需求调查,主要以调查问卷的方式进行.其实做问卷调查并不是想象中的那么简单,首先要确定问卷调查的内容,每一个问题都要经过深思熟虑,字字斟酌,既要切合问卷主要目的,又要简洁扼要 ...

  4. 从Adobe调查问卷看原型设计工具大战

    近年国内外原型设计工具新品频出,除了拥趸众多的老牌Axure在RP 8之后没有什么大的动作,大家都拼了命地在出新品.今天 inVision 的 Craft 出了 2.0 的预告视频,明天 Adobe ...

  5. Scrum立会报告+燃尽图(十一月十七日总第二十五次):设计调查问卷;修复上一阶段bug

    此作业要求参见:https://edu.cnblogs.com/campus/nenu/2018fall/homework/2284 项目地址:https://git.coding.net/zhang ...

  6. <问吧>调查问卷心得体会

    <问吧>调查问卷心得与体会 在这之前,我们已经组成了一个六个人的小团队---“走廊奔跑队”,我们这次做的这个项目的名称是:问吧.在项目实施之前,我们必做的一步就是需求分析,目的就是充分了解 ...

  7. android 实现调查问卷-单选-多选

    非常久没写东西了.今天来总结下有关android调查问卷的需求实现. 转载请加地址:http://blog.csdn.net/jing110fei/article/details/46618229 先 ...

  8. 自动化测试调查问卷送《QTP自动化测试最佳实践》

    自动化测试调查问卷送<QTP自动化测试最佳实践> http://automationqa.com/forum.php?mod=viewthread&tid=2308&fro ...

  9. HDU - 6344 2018百度之星资格赛 1001调查问卷(状压dp)

    调查问卷  Accepts: 1289  Submissions: 5642  Time Limit: 6500/6000 MS (Java/Others)  Memory Limit: 262144 ...

随机推荐

  1. Hadoop常用命令介绍

    本文主要介绍 Hadoop 常用的命令. test命令 用于检测文件或目录是否存在,判断文件或目录大小. -test -[defsz] <path> : Answer various qu ...

  2. 双目结构光三维扫描仪获得的三维点云模型(GIF)

  3. ssh自动添加hostkey到know_hosts

    The authenticity of host ‘git.sws.com (10.42.1.88)’ can’t be established. ECDSA key fingerprint is 5 ...

  4. WhatsApp Group vs WhatsApp Broadcast for Business

    WhatsApp Group vs WhatsApp Broadcast for Business By Iaroslav Kudritskiy If you've read our Ultimate ...

  5. Linux 基本权限管理

    1.linux权限表示: -rw-r--r-- (一共10位): 第一位:表示文件类型: 常用的三种文件类型:- 表示一般文件,d 表示目录,l 表示软链接文件: 后九位:每三位为一组: rwx r- ...

  6. 深度优先dfs与广度bfs优先搜索总结+例题

    DFS(Deep First Search)深度优先搜索 深度优先遍历(dfs)是对一个连通图进行遍历的算法.它的思想是从一个顶点开始,沿着一条路一直走到底,如果发现不能到达目标解,那就返回到上一个节 ...

  7. Git速成学习第二课:管理修改与删除文件

    Git速成学习笔记整理于廖雪峰老师的官网网站:https://www.liaoxuefeng.com/ 管理修改 首先我们需要明确,为什么说Git管理的是修改而不是文件呢? 我们首先对于已有的read ...

  8. manjar 搭建aria2c下载器

    从添加或删除软件管理程序里安装 aria2. 在一个地方创建一个文本文件,名为 「aria2.conf」 : rpc-user=我是用户名 rpc-passwd=我是密码 enable-rpc=tru ...

  9. 【Qt开发】关于QWSServer

    QWS Server QT Embeded应用没有来严格的区分server和client进程,如果一个QT进程的启动参数中有-qws,那么这个进程就具有server管理功能,被称为QWS server ...

  10. [转帖]Intel 上一代 可扩展CPU的简单报价

    8.1万元人间毒物!Intel 28核铂金版Xeon 8180零售上市 http://news.mydrivers.com/1/541/541670.htm 猜你想看:英特尔 CPU处理器 Xeon ...