vue+vuex的應用筆記

應用截圖

圖片描述

功能描述

填寫邀請碼註冊得到優惠券
一、填寫邀請碼
二、填寫聯繫方式
三、填寫帳號信息
四、完成註冊
2和3須要必填驗證

具體代碼

index.js

知識點:
加載vue、elementUi
輸出app、store
ES6模塊主要有兩個功能:export和import
export 用於對外輸出本模塊(一個文件能夠理解爲一個模塊)變量的接口
var name = 'lily';
var age = 19
export {
    name,
    age
}
import 用於在一個模塊中加載另外一個含有export接口的模塊。
也就是說使用export命令定義了模塊的對外接口之後,其餘JS文件就能夠經過import命令加載這個模塊(文件)
import {component1, component2} 按需引入,通過打包壓縮後文件體積更小
export與export default的區別
一、在一個文件或模塊中,export、import能夠有多個,export default僅有一個
二、經過export方式導出,在導入時要加{ },export default則不須要
import Vue from 'vue';
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';

import App from './app.vue';
import store from './store';

Vue.use(ElementUI);

export {
    App,
    store
};

app.vue

知識點:組件components
<template>
    <div class="register-page">
        <steps-bar/>
        <register-form/>
    </div>
</template>
    
    <script>
        import StepsBar from "./stepsBar.vue";
        import RegisterForm from "./registerForm.vue";
        
        export default {
            components: {
                'steps-bar': StepsBar,
                'register-form': RegisterForm,
            }
        };
    </script>
    <style lang="scss">
        html{
            min-width:auto;
            font-size: 100%;
        }
        .register-page{
            padding:20px;
        }
    </style>

registerForm.vue

知識點:
elementui表單及驗證
vuex的mapState
store的運用
<template>
<div class="register-box">
    <el-form ref="registerForm" :rules="registerFormRules" :model="registerForm" label-width="110px">
        <div class="step0" v-if="active == 0">
            <el-form-item label="邀請碼:" prop = "code">
                <el-input v-model="registerForm.code"></el-input>
            </el-form-item>
        </div>
        <div class="step1" v-if="active == 1">
            <el-form-item label="手機號:" prop="tel">
                <el-input v-model="registerForm.tel"></el-input>
            </el-form-item>
            <el-form-item label="短信驗證碼:" prop="smsCode">
                <el-input v-model="registerForm.smsCode" class="smsCodeInput"></el-input>
                <el-button @click.prevent="getSmsCode()">獲取驗證碼</el-button>
            </el-form-item>
        </div>
        <div class="step3" v-if="active == 2">
            <el-form-item label="企業名稱:" prop="ent">
                <el-input v-model="registerForm.ent"></el-input>
            </el-form-item>
            <el-form-item label="姓名:" prop="name">
                <el-input v-model="registerForm.name"></el-input>
            </el-form-item>
            <el-form-item label="密碼:" prop="password">
                <el-input type="password" v-model="registerForm.password"></el-input>
            </el-form-item>
        </div>
        <div class="step4 register-success" v-if="active == 3">
            <i class="el-icon-circle-check" style="font-size: 32px;"></i>
            <p>註冊成功</p>
        </div>
    </el-form>
    <div class="next-box" v-if="active<3">
        <el-button type="primary" @click="next" v-loading="registerLoading">下一步</el-button>
    </div>
</div>

</template>css

<script>html

import { mapState } from 'vuex';
import { NAMESPACE, NEXT_STEP} from './vuex';

export default {
    data(){
        return {
            registerForm: {
                code: 'sdada121212121',
                tel: '',
                smsCode: '',
                ent: '',
                name: '',
                password:''
            },
            registerFormRules: {
                tel:[
                    { required: true, message: '請輸入手機號碼', trigger: 'blur' },
                    { min: 11, max: 11, message: '請輸入正確的11位手機號碼', trigger: 'blur' }
                ],
                smsCode:[
                    { required: true, message: '請輸入短信驗證碼', trigger: 'blur' },
                ],
                ent:[
                    { required: true, message: '請輸入企業名稱', trigger: 'blur' },
                ],
                name:[
                    { required: true, message: '請輸入姓名', trigger: 'blur' },
                ],
                password:[
                    { required: true, message: '請輸入密碼', trigger: 'blur' },
                ],
            }
        }
    },
    computed:{
        ...mapState({
            active: state => state.register.stepsAction,
            registerLoading: state => state.register.registerLoading
        })
    },
    methods: {
        validate(callback) {
            this.$refs['registerForm'].validate((valid) => {
                if (valid) {
                    callback && callback(this.registerForm);
                } else {
                    console.log('error submit!!');
                    return false;
                }
            });
        },
        next() {
            this.validate((data)=>{
                if (this.active === 2) { // 提交註冊
                    console.log(data);
                    this.$store.dispatch(`${NAMESPACE}/registerSave`, data);
                } else {
                    this.$store.commit(`${NAMESPACE}/${NEXT_STEP}`, 1);
                }
            });
            }
        }
    };
</script>

<style lang="scss" scoped>
    .register-box{
        margin:60px 20px;
    }
    .register-success{
        font-size: 32px;
        display:flex;
        justify-content: center;
        align-items: center;
    }
    .next-box{
        display:flex;
        justify-content:center;
        align-items:center;
        margin:60px 0 10px;
    }
    .smsCodeInput{
        width: calc(100% - 118px);
    }
</style>
stepsBar.vue
<template>
    <el-steps :active="active" align-center>
        <el-step title="邀請碼"></el-step>
        <el-step title="聯繫方式"></el-step>
        <el-step title="帳號信息"></el-step>
        <el-step title="完成註冊"></el-step>
    </el-steps>
</template>

<script>
    import {mapState} from 'vuex';

    export default {
        computed:{
            ...mapState({
                active: state => state.register.stepsAction
            })
        }
    };
</script>

store.js

知識點:
store拆分即多模塊狀態管理(modules)
//定義方法
export const NAMESPACE = 'register';
export default {
    namespaced: true,
    state,
    mutations,
    actions
};
// 使用方法
import { mapState } from 'vuex';
import { NAMESPACE, NEXT_STEP} from './vuex';
//監聽響應式屬性變化
computed:{
    ...mapState({
        active: state => state.register.stepsAction
    })
}
//觸發actions
this.$store.dispatch(`${NAMESPACE}/registerSave`, data); 
//觸發mutations
this.$store.commit(`${NAMESPACE}/${NEXT_STEP}`, 1);

//store.js
import Vue from 'vue';
import Vuex from 'vuex';

Vue.use(Vuex);

import register from './vuex.js';
const store = new Vuex.Store({
    modules:{
        register
    }
});

export default store;

vuex.js

知識點:
對象的解構賦值
const node = {
  loc: {
    start: {
      line: 1,
      column: 5
    }
  }
};

let { loc, loc: { start }, loc: { start: { line }} } = node;
line // 1
loc  // Object {start: Object}
start // Object {line: 1, column: 5}
vuex NAMESPACE:
vuex中的store分模塊管理,須要在store的index.js中引入各個模塊,爲了解決不一樣模塊命名衝突的問題,將不一樣模塊的namespaced:true,以後在不一樣頁面中引入getter、actions、mutations時,須要加上所屬的模塊名
mutations和actions的區別
Vuex中store數據改變的惟一方法就是mutation
actions 只是一個架構性的概念,並非必須的,說到底只是一個函數,你在裏面想幹嗎均可以,只要最後觸發 mutation 就行。異步競態怎麼處理那是用戶本身的事情。vuex 真正限制你的只有 mutation 必須是同步的這一點

使用常量替代 Mutation 事件類型vue

//vuex.js
import { Message } from 'element-ui';

import api from '@/api';
const { website: { register: { register: { registerHandle } } } } = api;

export const NAMESPACE = 'register';

export const NEXT_STEP = 'NEXT_STEP';
export const SUBMIT_LOADING = 'SUBMIT_LOADING';
export const SUBMIT_SUCCESS = 'SUBMIT_SUCCESS';
export const SUBMIT_ERROR = 'SUBMIT_ERROR';

const state = {
    stepsAction: 0,
    registerLoading: false
};

const mutations = {
    [NEXT_STEP](state, n) {
        state.stepsAction = state.stepsAction + n;
    },
    [SUBMIT_LOADING](state) {
        state.registerLoading = true;
    },
    [SUBMIT_SUCCESS](state) {
        state.registerLoading = false;
        state.stepsAction = state.stepsAction + 1;
    },
    [SUBMIT_ERROR](state, res) {
        state.registerLoading = false;
        Message.error(res.msg);
    }
};

const actions = {
    registerSave({ commit, state }, data) {
        commit(SUBMIT_LOADING);
        registerHandle(data).then((response) => {
            if (response.status === 200) {
                const responseData = response.data;
                const result = responseData.result;
                if (responseData.status === 200) {
                    commit(SUBMIT_SUCCESS, result);
                } else {
                    commit(SUBMIT_ERROR, {
                        msg: responseData.msg
                    });
                }
            }
        }).catch(() => {
            commit(SUBMIT_ERROR, { msg: '服務器請求錯誤' });
        });
    }
};

export default {
    namespaced: true,
    state,
    mutations,
    actions
};
相關文章
相關標籤/搜索