老司机91精品网站在线观看-老司机67194免费观看-老司国产精品视频-老熟人老女人国产老太-中文字幕日本在线-中文字幕日本一区久久

vue.js路由與vuex數據模型設計

2020-5-23    seo達人

路由設計

本則路由考慮驗證進入登錄頁面,完成登錄操作進入首頁。


import Vue from "vue";

import Router from "vue-router";

Vue.use(Router);


import store from "@/store/store";


// (延遲加載)

const Login = () => import("@/views/login");

const Home = () => import("@/views/home");


const HomeRoute = {

 path: "/",

 name: "首頁",

 component: Home

};


export { HomeRoute };


const router = new Router({

 base: process.env.BASE_URL,

 routes: [

   {

     path: "/login",

     name: "登錄",

     component: Login

   },

   HomeRoute

 ]

});


router.beforeEach((to, from, next) => {

 let loginName = store.state.user.loginName;

 if (to.path === "/" && loginName == "") {

   next("/login");

 } else {

   next();

 }

});


export default router;

數據模型

const state = {

 loginName: ""

};

const mutations = {

 SET_LOGINNAME(state, loginName) {

   state.loginName = loginName;

 }

};

const actions = {

 login({ commit }, userInfo) {

   return new Promise((res, ret) => {

     commit("SET_LOGINNAME", userInfo);

     res();

   });

 },

 logout({ commit }) {

   return new Promise((res, ret) => {

     commit("SET_LOGINNAME", "");

     res();

   });

 }

};

export default {

 namespaced: true,

 state,

 mutations,

 actions

};

import Vue from "vue";

import Vuex from "vuex";

Vue.use(Vuex);


import user from "./modules/user";


const store = new Vuex.Store({

 modules: {

   user

 }

});


export default store;

組件

<div class="modify">

 <input

   type="text"

   @keydown.enter.prevent="handleKeydown"

   v-model="currentVal"

   placeholder="使用enter鍵切換頻道"

 />

 <button @click="reset" style="margin-left:5px;outline:none;cursor:pointer;">復位</button>

</div>

import { mapState, mapMutations, mapActions } from "vuex";

export default {

 name: "login",

 data() {

   return {

     currentVal: "",

     list: ["咨詢服務", "音悅臺", "體育臺", "財經頻道", "時尚資訊"],

     index: 0

   };

 },

 computed: {

   ...mapState({

     loginName: state => state.user.loginName

   })

 },

 methods: {

   ...mapActions({

     login: "user/login"

   }),

   handleToHome() {

     let userInfo = "user";

     this.login(userInfo);

     this.$router.push({

       path: "/"

     });

   },

日歷

鏈接

個人資料

藍藍設計的小編 http://www.hk66688.com

存檔

主站蜘蛛池模板: 安塞县| 宁化县| 黄山市| 潍坊市| 海晏县| 富源县| 常熟市| 辉南县| 加查县| 武强县| 灵丘县| 寻乌县| 吐鲁番市| 隆回县| 黔江区| 盐源县| 上饶市| 英山县| 永新县| 大化| 加查县| 平远县| 广平县| 蒙山县| 连平县| 灌南县| 津市市| 丰原市| 江口县| 平舆县| 内江市| 酒泉市| 慈溪市| 崇左市| 腾冲县| 蒲江县| 渝中区| 孟津县| 大兴区| 塔城市| 上思县|