" />

飞道的博客

前端模拟登录注册静态实现示例

487人阅读  评论(0)

点击蓝色 “达达前端” 关注我哦!

加个 “星标” ,每天一篇文章,一起学编程

登录注册,说说登录,需要用户名,用户名的提示内容为请输入用户名,密码的提示为8-18位不含特殊字符的数字、字母组合。还有一个点击按钮。


   
  1. <view  class="input-content">
  2.   <view  class="input-item">
  3.     <text  class="username">用户名</text>
  4.     <input v-model= "query.username" placeholder= "请输入用户名"/>
  5. < /view>
  6. <view class="input-item">
  7.     <text class="password">密码</text>
  8.     <input v-model= "query.password" 
  9.     placeholder= "8-18位不含特殊字符的数字、字母组合"
  10.     maxlength= "20"
  11.     password />
  12. < /view>
  13. </view
  14. <button  class="confirm-btn" @click="toLogin" >登录</button>

   
  1. <html>
  2. <head>
  3. <link rel="stylesheet" href="index.css">
  4. </head>
  5. <body>
  6. <div id="app">
  7. {{count}}
  8. <button @click='increment'>+ </button>
  9. </div>
  10. <script src="index.pack.js"> </script>
  11. </body>
  12. </html>

   
  1. import Vue from 'vue';
  2. import Vuex from 'vuex';
  3. Vue.use(Vuex);
  4. const store = new Vuex.Store({
  5. state: {
  6. count: 0
  7. },
  8. mutations: {
  9. }
  10. });
  11. import { mapState } from 'vuex';
  12. new Vue({
  13. el: '#app',
  14. store,
  15. data: {
  16. },
  17. computed: mapState([
  18. 'count'
  19. ]),
  20. });

vuex是专为vue.js应用程序开发的状态管理模式,它采用集中式存储管理应用的所有组件的状态,并以相应的规则状态以一种可预测的方式发生变化。

什么是状态管理模式?


   
  1. new Vue({
  2. // state
  3. data () {
  4. return {
  5. count: 0
  6. }
  7. },
  8. // view
  9. template: `
  10. <div>{{ count }}</div>
  11. `,
  12. // actions
  13. methods: {
  14. increment () {
  15. this.count++
  16. }
  17. }
  18. })

state为驱动应用的数据源,view为以声明方式将state映射到视图,actions为响应在view上的用户输入导致的状态变化。

当我们的应用遇到多个组件共享状态时,单向数据流的简洁性很容易被破坏。

第一,多个视图依赖于同一状态。

第二,来自不同的视图的行为需要变更同一状态。

第一种情况,传参的方法对于多层嵌套的组件将会非常繁琐,并且对于兄弟组件间的状态传递无能为力。

第二种情况,我们会采用父子组件直接引用或者通过事件来变更和同步状态的多份拷贝。

可以把组件的共享状态抽取出来,以一个全局单例模式管理。这样,组件树构成了一个巨大的“视图”,不管在树的哪个位置,任何组件都能获取状态或者触发行为。

通过定义和隔离状态管理中各种概念并通过强制规则维持视图和状态间的独立性。

vuex是专门为vue.js设计的状态管理库,以利用vue.js的细粒度数据响应机制来进行高效的状态更新。

每个vuex应用的核心就是store仓库,store就是一个容器,包含着大部分的状态。

vuex的状态存储是响应式的,当vue组件从store中读取状态的时候,如果store中的状态发生变化,那么相应的组件也会相应地得到更新。

不能直接改变store中的状态,改变store中的状态的唯一途径就是显式地提交commit mutation,可以方便跟踪每一个状态的变化。

Store的认识

安装Vuex后,让我们来创建一个store。


   
  1. const store =  new Vuex.Store({
  2. state: {
  3.    count0
  4. },
  5. mutations: {
  6.   increment(state) {
  7.    state.count++
  8.   }
  9.  }
  10. })

可以通过store.state来获取状态对象,然后通过store.commit方法触发状态的变更。


   
  1. store .commit(' increment');
  2. console .log( store .state .count);

通过提交mutation的方式,而非直接改变store.state.count。

核心概念:State,Getter,Mutation,Action,Module。

State单一状态


   
  1. <html>
  2. <head>
  3. <link rel="stylesheet" href="index.css">
  4. </head>
  5. <body>
  6. <div id="app">
  7. {{ count }}
  8. </div>
  9. <script src="index.pack.js"> </script>
  10. </body>
  11. </html>

   
  1. import Vue from 'vue';
  2. import Vuex from 'vuex';
  3. Vue.use(Vuex);
  4. const store = new Vuex.Store({
  5. state: {
  6. count: 0
  7. }
  8. });
  9. new Vue({
  10. el: '#app',
  11. store,
  12. computed: {
  13. count () {
  14. return this.$store.state.count
  15. }
  16. }
  17. });

单一状态树,用一个对象包含了全部的应用层级状态。


   
  1. // 创建一个 Counter 组件
  2. const Counter = {
  3. template: `<div>{{ count }}</div>`,
  4. computed: {
  5. count () {
  6. return store.state.count
  7. }
  8. }
  9. }

   
  1. const app = new Vue({
  2. el: '#app',
  3. // 把 store 对象提供给 “store” 选项,这可以把 store 的实例注入所有的子组件
  4. store,
  5. components: { Counter },
  6. template: `
  7. <div class= "app">
  8. <counter></counter>
  9. </div>
  10. `
  11. })

通过在根实例总注册store,该store实例会注入根组件下的所有子组件中,且子组件能通过this.$store访问到。


   
  1. const Counter = {
  2. template: `<div>{{ count }}</div>`,
  3. computed: {
  4. count () {
  5. return this.$store.state.count
  6. }
  7. }
  8. }

mapState辅助函数


   
  1. // 在单独构建的版本中辅助函数为 Vuex.mapState
  2. import { mapState } from 'vuex'
  3. export default {
  4. // ...
  5. computed: mapState({
  6. // 箭头函数可使代码更简练
  7. count: state => state.count,
  8. // 传字符串参数 'count' 等同于 `state => state.count`
  9. countAlias: 'count',
  10. // 为了能够使用 `this` 获取局部状态,必须使用常规函数
  11. countPlusLocalState (state) {
  12. return state.count + this.localCount
  13. }
  14. })
  15. }

当映射的计算属性的名称与 state 的子节点名称相同时。


   
  1. computed: mapState([
  2. // 映射 this.count 为 store.state.count
  3. 'count'
  4. ])

对象展开运算符

mapState函数返回的是一个对象。


   
  1. computed: {
  2. localComputed () { /* ... */ },
  3. // 使用对象展开运算符将此对象混入到外部对象中
  4. ...mapState({
  5. // ...
  6. })
  7. }

Getter


   
  1. computed: {
  2. doneTodosCount () {
  3. return this.$store.state.todos.filter( todo => todo.done).length
  4. }
  5. }

getter的返回值会根据它的依赖被缓存起来,且只有当它的依赖值发生了改变才会被重新计算。


   
  1. const store = new Vuex.Store({
  2. state: {
  3. todos: [
  4. { id: 1, text: '...', done: true },
  5. { id: 2, text: '...', done: false }
  6. ]
  7. },
  8. getters: {
  9. doneTodos: state => {
  10. return state.todos.filter( todo => todo.done)
  11. }
  12. }
  13. })

通过属性访问

Getter会暴露store.getters对象。

store.getters.doneTodos // -> [{ id: 1, text: '...', done: true }]

   
  1. getters: {
  2. // ...
  3. doneTodosCount: (state, getters) => {
  4. return getters.doneTodos.length
  5. }
  6. }
  7. store.getters.doneTodosCount
  8. computed: {
  9. doneTodosCount () {
  10. return this.$store.getters.doneTodosCount
  11. }
  12. }

通过方法访问


   
  1. getters: {
  2. // ...
  3. getTodoById: (state) => (id) => {
  4. return state.todos.find( todo => todo.id === id)
  5. }
  6. }
  7. store.getters.getTodoById( 2)

mapGetters辅助函数

mapGetters辅助函数仅仅是将store中的getter映射到局部计算属性。


   
  1. import { mapGetters } from 'vuex'
  2. export default {
  3. // ...
  4. computed: {
  5. // 使用对象展开运算符将 getter 混入 computed 对象中
  6. ...mapGetters([
  7. 'doneTodosCount',
  8. 'anotherGetter',
  9. // ...
  10. ])
  11. }
  12. }

   
  1. mapGetters({
  2. //`this.doneCount` 映射为 `this.$store.getters.doneTodosCount`
  3. doneCount: 'doneTodosCount'
  4. })

vuex,vue本身自带有store模式,其实就是全局注册一个对象,实现数据共享,适合小型数据少的项目。

vuex的五大核心,state,getters,mutations,actions,module。

vuex四大辅助函数,mapState,mapGetters,mapMutations,mapActions。

vuex的工作流程

客户端操作事件,dispatch调用一个action。

对应的action处理参数,比如接口,逻辑操作,传值,commit的type类型,mutation介绍type类型触发对象的函数,修改state,state更新后中view视图在render的作用下重新渲染。

mapState和mpaGetter的使用只能在computed计算属性中。

mapMutations和mapActions使用的额时候只能在methods中调用。


   
  1. <script>
  2. import { mapState , mapMutations , mapActions , mapGetters } from 'vuex';
  3. export default {
  4. data(){
  5. return{
  6. }
  7. },
  8. computed:{
  9. ...mapState({
  10. counts: (state) => state.count
  11. }),
  12. //mapState就等于下面这个
  13. // counts(){
  14. // return this.$store.state.count
  15. // },
  16. ...mapGetters({
  17. getternum: 'doneTodos'
  18. }),
  19. //mapGetters就等于下面的这个
  20. // getternum(){
  21. // return this.$store.getters.doneTodos
  22. // }
  23. },
  24. methods:{
  25. ...mapMutations({
  26. addnum: 'addNum'
  27.     }),
  28. //mapMutations就等于下面的这个
  29. // addnum1(){
  30. // this.$store.commit('addNum')
  31. // },
  32. ...mapActions({
  33. actionnum: 'actionNumAdd'
  34. }),
  35. //mapActions就等于下面的这个
  36. // actionnum6(){
  37. // this.$store.dispatch('actionNumAdd')
  38. // }
  39. }
  40. }
  41. </script>
调用 方法 辅助函数
state this.$store.state. xxx mapState
getters this.$store.getters. xxx mapGetters
mutations this.$store.cmmit((xxx) mapMutations
actions this.$store.dispatch(xxx ) mapActions


states.js


   
  1. const state = {
  2. count: 0
  3. }
  4. export  default state

getter.js


   
  1. const getters = {
  2. docount: (state,getters) => {
  3. return state.counts
  4. }
  5. }
  6. export  default getters

Mutation

更改vuex的store中的状态的唯一方法是提交mutation。每个mutation都有一个字符串的事件类型和一个回调函数。


   
  1. const store = new Vuex.Store({
  2. state: {
  3. count: 1
  4. },
  5. mutations: {
  6. increment (state) {
  7. // 变更状态
  8. state.count++
  9. }
  10. }
  11. })
store.commit('increment')

提交载荷


   
  1. // ...
  2. mutations: {
  3. increment (state, n) {
  4. state.count += n
  5. }
  6. }
  7. store.commit( 'increment', 10)

载荷大多数是一个对象


   
  1. // ...
  2. mutations: {
  3. increment (state, payload) {
  4. state.count += payload.amount
  5. }
  6. }
  7. store.commit('increment', {
  8. amount: 10
  9. })

对象风格的提交


   
  1. store .commit({
  2. type: 'increment',
  3. amount: 10
  4. })
  5. mutations: {
  6. increment (state, payload) {
  7. state.count += payload.amount
  8. }
  9. }

对象展开运算符

state.obj = { ...state.obj, newProp: 123 }

在组件中提交Mutation


   
  1. this.$store.commit( 'xxx')
  2. 使用 mapMutations 辅助函数
  3. 将组件中的 methods 映射为 store.commit 调用

   
  1. import { mapMutations } from 'vuex'
  2. export default {
  3. // ...
  4. methods: {
  5. ...mapMutations([
  6. 'increment', // 将 `this.increment()` 映射为 `this.$store.commit('increment')`
  7. // `mapMutations` 也支持载荷:
  8. 'incrementBy' // 将 `this.incrementBy(amount)` 映射为 `this.$store.commit('incrementBy', amount)`
  9. ]),
  10. ...mapMutations({
  11. add: 'increment' // 将 `this.add()` 映射为 `this.$store.commit('increment')`
  12. })
  13. }
  14. }

在vuex中,mutation都是同步事务:


   
  1. store.commit( 'increment')
  2. // 任何由 "increment" 导致的状态变更都应该在此刻完成。

Action,action提交的是mutation,而不是直接变更状态。action可以包含任意异步操作。


   
  1. const store = new Vuex.Store({
  2. state: {
  3. count: 0
  4. },
  5. mutations: {
  6. increment (state) {
  7. state.count++
  8. }
  9. },
  10. actions: {
  11. increment (context) {
  12. context.commit('increment')
  13. }
  14. }
  15. })

   
  1. actions: {
  2. increment ({ commit }) {
  3. commit('increment')
  4. }
  5. }

分发action

action通过store.dispatch分发触发:

store.dispatch('increment')

可以在action内部执行异步操作。


   
  1. actions: {
  2. incrementAsync ({ commit }) {
  3. setTimeout( () => {
  4. commit( 'increment')
  5. }, 1000)
  6. }
  7. }

actions支持载荷方式和对象方式:


   
  1. // 以载荷形式分发
  2. store.dispatch('incrementAsync', {
  3. amount: 10
  4. })
  5. // 以对象形式分发
  6. store.dispatch({
  7. type: 'incrementAsync',
  8. amount: 10
  9. })

在组件中分发action


   
  1. this.$store.dispatch( 'xxx') 分发 action
  2. 使用 mapActions 辅助函数将组件的 methods 映射为 store.dispatch 调用

   
  1. import { mapActions } from 'vuex'
  2. export default {
  3. // ...
  4. methods: {
  5. ...mapActions([
  6. 'increment', // 将 `this.increment()` 映射为 `this.$store.dispatch('increment')`
  7. // `mapActions` 也支持载荷:
  8. 'incrementBy' // 将 `this.incrementBy(amount)` 映射为 `this.$store.dispatch('incrementBy', amount)`
  9. ]),
  10. ...mapActions({
  11. add: 'increment' // 将 `this.add()` 映射为 `this.$store.dispatch('increment')`
  12. })
  13. }
  14. }

module

vuex将store分割成模块,每个模块拥有自己的state,mutation,action,getter。


   
  1. const moduleA = {
  2. state: { ... },
  3. mutations: { ... },
  4. actions: { ... },
  5. getters: { ... }
  6. }
  7. const moduleB = {
  8. state: { ... },
  9. mutations: { ... },
  10. actions: { ... }
  11. }
  12. const store = new Vuex.Store({
  13. modules: {
  14. a: moduleA,
  15. b: moduleB
  16. }
  17. })
  18. store.state.a // -> moduleA 的状态
  19. store.state.b // -> moduleB 的状态

login.vue


   
  1. <view class= "input-content">
  2. <view class="input-item">
  3. <text class="tit">用户名 </text>
  4. <input v-model="query.username" placeholder="请输入用户名" />
  5. </view>
  6. <view class="input-item">
  7. <text class="tit">密码 </text>
  8. <input v-model="query.password" placeholder="8-18位不含特殊字符的数字、字母组合" maxlength="20"
  9. password @ confirm= "toLogin" />
  10. </view>
  11. </view>
  12. <button class="confirm-btn" @click="toLogin" :disabled="logining">登录 </button>

   
  1. import {
  2. mapMutations
  3. }
  4. from 'vuex';

   
  1. mobile: '', // 手机号
  2. password: '', // 密码
  3. logining:  false ,
  4. ...mapMutations([ 'login']),
  5. toLogin() {
  6. this.logining = true;
  7. loginApi( this.query).then( res => {
  8. if(res.data.code === 1) {
  9. this.$api.msg(res.data.msg)
  10. ...
  11. }).catch( res => {
  12. this.logining = false;
  13. })
  14. }

store


   
  1. import Vue from 'vue'
  2. import Vuex from 'vuex'
  3. Vue.use(Vuex)
  4. const store = new Vuex.Store({
  5. state: { //全局变量定义处
  6. hasLogin: false, //用户是否登录
  7.     userInfo: {},  //用于存放用户账户数据
  8. },
  9. mutations: { //全局方法定义处
  10. login(state, provider) {
  11. },
  12. logout(state) {
  13. },
  14. },
  15. actions: {
  16. }
  17. })
  18. export default store

☆ END ☆

参考文档来源:vuex官方文档https://vuex.vuejs.org/zh/

加群前端交流群

扫码,备注 加群-技术领域-城市-姓名 

目前文章内容涉及前端知识点,囊括Vue、JavaScript、数据结构与算法、实战演练、Node全栈一线技术,紧跟业界发展步伐,将 Web前端领域、网络原理等通俗易懂的呈现给小伙伴。更多内容请到达达前端网站进行学习:www.dadaqianduan.cn

1、你知道多少this,new,bind,call,apply?那我告诉你

2、为什么学习JavaScript设计模式,因为它是核心

3、一篇文章把你带入到JavaScript中的闭包与高级函数

4、大厂HR面试ES6中的深入浅出面试题知识点

觉得本文对你有帮助?请分享给更多人

关注「达达前端」加星标,提升前端技能

这是一个有质量,有态度的公众号


转载:https://blog.csdn.net/qq_36232611/article/details/104305628
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场