• vue全家桶router、vuex、axios


    main.js

    import Vue from 'vue'
    import App from './App'
    import router from './router'
    import store from './vuex'
    import Axios from 'axios'
    
    Vue.prototype.$axios=Axios
    
    Axios.defaults.baseURL="http://www.wwtliu.com"
    
    Vue.config.productionTip = false
    
    new Vue({
        el: '#app',
        router,
        store,
        components: {App},
        template: '<App/>'
    })

    一、router

    import Vue from 'vue'
    import Router from 'vue-router'
    import all from '@/components/all'
    
    Vue.use(Router)
    
    const router = new Router({
      mode: "history",//nginx需配置:server{location / {try_files $uri $uri/ /index.html;}}
      routes: [
        {
          path: '/',
          redirect: "/all"
        },
        {
          path: '/all',
          component: all
        },
        {
          path: '/all/:data',
          component: all
        },
        {
          path: '/all',
          component: all,
          children: [
            {
              path: '/all/first',
              component: first,
            },
            {
              path: 'second',
              component: second,
            }
          ]
        }
      ]
    })
    
    export default router;
    <router-link to="/all" tag="li">全部</router-link>
    
    <router-view/>
    this.$router.push({name:'',path:'',query:{data:''},params:{data:''}})
    
    this.$route.query.data
    this.$route.params.data

    路由守卫:

    beforeRouteEnter(to,from,next){
        if(false){
            next();
        }else{
            next("/login");
        }
    }
    
    router.beforeEach((to,from,next)=>{
        if(to.path=="/info" && false){
            next()
        }else{
            next("/login")
        }
    })

    路由监听:

    watch: {
      "$route.path": function () {
      }
    },

    二、vuex

    import Vue from "vue"
    import Vuex from "vuex"
    
    Vue.use(Vuex)
    
    const store = new Vuex.Store({
        state: {
            list:[]
        },
        mutations: {
            add(state,arg){
                state.list.push(arg);
            }
        },
        getters:{
            activelist(state){
                return state.list.filter(item=>item.tasksty)
            }
        },
        actions:{
            //异步处理
            saveDataAction(arg_store,data){
                arg_store.commit('add',data);
            }
        }
    })
    
    export default store;
    this.$store.dispatch("saveDataAction",'data_data_data');
    <input type="text" placeholder="你打算做什么?"  @keyup.enter="modelvalue">
    
    methods:{
        modelvalue(ev){
            this.$store.commit("add",{taskval:ev.target.value,tasksty:false});
        }
    }
    <li v-for="li in list"></li>
    <li v-for="li in activelist"></li>
    
    import {mapState,mapGetters} from "vuex"
    computed:{ 
        ...mapState(["list"]),
        ...mapGetters(["activelist"])
    }

    vuex持久化

    1、安装
      npm i -S vuex-persistedstate
    2、配置
      import persistedState from 'vuex-persistedstate'
      const store = new Vuex.Store({
        plugins: [persistedState({storage: window.sessionStorage})]
      })

    vuex命名空间

    import Vue from "vue"
    import Vuex from "vuex"
    
    Vue.use(Vuex)
    
    let storemodule = {
        namespaced: true,
        state: {
            storemodulestate: ""
        },
        mutations: {
            setStoremodulestate(state, arg) {
                state.storemodulestate = arg
            }
        },
        getters: {
            getStoremodulestate(state) {
                return state.storemodulestate
            }
        }
    }
    
    export default new Vuex.Store({
        modules: {storemodule}
    })
    <template>
        <div>
        </div>
    </template>
    
    <script>
        import {mapGetters} from "vuex"
    
        export default {
            name: "index",
            computed: {
                ...mapGetters({
                    storemodulestate: "storemodule/getStoremodulestate",
                    modulestate: "storemodule/getStoremodulestate"
                })
            },
            mounted() {
                console.log(this.storemodulestate)
                this.$store.commit("storemodule/setStoremodulestate", "setStoremodulestate")
                console.log(this.modulestate)
            }
        }
    </script>
    
    <style scoped>
    
    </style>

    三、axios

    this.$axios.post('/user', {
      firstName: 'Fred',
      lastName: 'Flintstone'
    }, {
      headers: {
        'token': ''
      },
      responseType: 'blob'
    }).then(function (response) {
      console.log(response);
      let blob = new Blob([response.data], {type: "application/vnd.ms-excel,charset=utf-8"});
      let link = window.URL.createObjectURL(blob);
      let element = document.createElement("a");
      element.href = link;
      element.download = "文件名.xlsx";
      document.body.appendChild(element);
      element.click();
      window.URL.revokeObjectURL(link);
      document.body.removeChild(element);
    }).catch(function (error) {
      console.log(error);
    });
    
    this.$axios.get('/user', {
      headers: {
        'token': ''
      },
      responseType: 'blob',
      params:{
        firstName: 'Fred',
        lastName: 'Flintstone'
      }
    }).then(function (response) {
      console.log(response);
    }).catch(function (error) {
      console.log(error);
    });
    this.$axios.interceptors.request.use(req => {
        return req;
    },err => {
        return Promise.reject(err);
    })
    
    this.$axios.interceptors.response.use(res => {
        return res;
    },err => {
        return Promise.reject(err);
    })
  • 相关阅读:
    SNF快速开发平台MVC-名片管理(实际名片样式)
    SNF快速开发平台MVC-表格单元格合并组件
    SNF快速开发平台MVC-单据状态水印
    SNF快速开发平台MVC-瀑布式分页组件
    SNF快速开发平台MVC-高级查询组件
    SNF快速开发平台MVC-自由排序组件
    SNF快速开发平台MVC-各种级联绑定方式,演示样例程序(包含表单和表格控件)
    SNF快速开发平台MVC-集成了百度开源项目echars
    SNF开发平台WinForm-平板拍照及扫描二维码功能
    SNF快速开发平台--规则引擎整体介绍及使用说明书
  • 原文地址:https://www.cnblogs.com/linding/p/12375101.html
Copyright © 2020-2023  润新知