<address id="ttjl9"></address>

      <noframes id="ttjl9"><address id="ttjl9"><nobr id="ttjl9"></nobr></address>
      <form id="ttjl9"></form>
        <em id="ttjl9"><span id="ttjl9"></span></em>
        <address id="ttjl9"></address>

          <noframes id="ttjl9"><form id="ttjl9"></form>

          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.syprn.cn

          存檔

          亚洲va欧美va天堂v国产综合