美文网首页前端开发
Vue全家桶多级嵌套

Vue全家桶多级嵌套

作者: 小程序前端超市 | 来源:发表于2019-01-29 18:45 被阅读8次

模块安装

$ npm install vue vue-router vuex axios -S

目录结构

src
  ├─ pages
  |  ├─ VehicleManage
  |  |  ├─ VehicleRegisterForm
  |  |  |  ├─ BaseInfo
  |  |  |  |  └─ index.vue
  |  |  |  └─ index.vue
  |  └─ └─ index.vue
  ├─ router
  |  ├─ vehicleManage
  |  |  ├─ vehicleRegisterForm
  |  |  |  └─ index.js
  |  |  └─ index.js
  |  └─ index.js
  ├─ store
  |  ├─ vehicleManage
  |  |  ├─ vehicleRegisterForm
  |  |  |  └─ index.js
  |  |  └─ index.js
  |  └─ index.js
  ├─ App.vue
  └─ main.js

main.js

import Vue from 'vue';
import router from './router';
import store from './store';
import axios from 'axios';

import App from './App.vue';

Vue.config.productionTip = false;

Vue.prototype.$axios = axios;

new Vue({
    router,
    store,
    render: h => h(App),
}).$mount('#app')

App.vue

<template>
    <div id="app">
        <router-view></router-view>
    </div>
</template>
<script>
export default {
    name: 'App'
}
</script>

路由

router/index.js

import Vue from 'vue';
import Router from 'vue-router';
import vehicleManage from './vehicleManage/index';

Vue.use(Router);

export default new Router({
    mode: 'hash',
    routes: [
        {
            path: '/',
            name: 'Home',
            redirect: 'vehicleManage/vehicleRegisterForm/baseInfo'
        },
        // 车辆管理
        vehicleManage
    ]
});

router/vehicleManage/index.js

/* 车辆管理 */
import VehicleManage from '@/pages/VehicleManage';
// 车辆登录表
import VehicleRegisterForm from './vehicleRegisterForm/index';

export default {
    path: '/vehicleManage',
    name: 'vehicleManage',
    component: VehicleManage,
    children: [
        VehicleRegisterForm
    ]
}
router/vehicleManage/vehicleRegisterForm/index.js
/* 车辆登记表 */
import VehicleRegisterForm from '@/pages/VehicleManage/VehicleRegisterForm';
// 基本信息
import BaseInfo from '@/pages/VehicleManage/VehicleRegisterForm/BaseInfo';

export default {
    path: 'vehicleRegisterForm',
    name: 'vehicleRegisterForm',
    component: VehicleRegisterForm,
    children: [
        {
            path: 'baseInfo',
            name: 'baseInfo',
            component: BaseInfo
        }
    ]
}

Vuex Store

store/index.js

import Vue from 'vue';
import Vuex from 'vuex';
import vehicleManage from './vehicleManage/index';

Vue.use(Vuex);

export default new Vuex.Store({
    state: {
        msg: 'aaa'
    },
    mutations: {

    },
    actions: {

    },
    modules: {
        vehicleManage
    }
})

store/vehicleManage/index.js

import vehicleRegisterForm from './vehicleRegisterForm/index';
export default {
    namespaced: true,
    state: {
        msg: 'bbb'
    },
    getter: {

    },
    mutations: {

    },
    actions: {

    },
    modules: {
        vehicleRegisterForm
    }
}
store/vehicleManage/vehicleRegisterForm/index.js
export default {
    namespaced: true,
    state: {
        msg: 'ccc'
    },
    getter: {

    },
    mutations: {

    },
    actions: {

    }
}

页面

pages/VehicleManage/index.vue

<template>
    <router-view></router-view>
</template>
<script>
export default {
    name: 'VehicleManage'
}
</script>

pages/VehicleManage/VehicleRegisterForm/index.vue

<template>
    <router-view></router-view>
</template>
<script>
export default {
    name: 'VehicleRegisterForm'
}
</script>
pages/VehicleManage/VehicleRegisterForm/BaseInfo/index.vue
<template>
    <div>
        hello world ~
    </div>
</template>
<script>
import {mapState} from 'vuex';
export default {
    name: 'BaseInfo',
    data() {
        return {
            // ...
        }
    },
    computed: {
        ...mapState([
            'msg'
        ]),
        ...mapState('vehicleManage', {
            msg2: (state) => {
                return state.msg;
            }
        }),
        ...mapState('vehicleManage/vehicleRegisterForm', {
            msg3: (state) => {
                return state.msg;
            }
        })
    },
    mounted() {
        console.log(this.msg, this.msg2,this.msg3); // aaa bbb ccc
    }
}
</script>

相关文章

  • Vue全家桶多级嵌套

    模块安装 目录结构 main.js App.vue 路由 router/index.js router/vehic...

  • 2019-03-25

    vue 全家桶 Vue有著名的全家桶系列,包含了: vue-router,vuex,vue-re...

  • 2018-12-13

    vue的v-for嵌套可以多级引用,不同标示符就可以

  • 前端面试2021.4.9

    面试题正文: 1.vue全家桶包含哪些? 答案:vue全家桶与react全家桶介绍 2.v-model是什么?怎么...

  • 前端面试2021.4.9

    面试题正文: 1.vue全家桶包含哪些? 答案:vue全家桶与react全家桶介绍 2.v-model是什么?怎么...

  • Vue生态圈(全家桶)

    vue全家桶及项目架构 Vue有著名的全家桶系列,包含了vue-router(http://router.vuej...

  • vue + typescript

    Vue全家桶+TypeScript使用总结

  • iOS 多级 UIScrollView 嵌套的实现方案

    iOS 多级 UIScrollView 嵌套的实现方案 iOS 多级 UIScrollView 嵌套的实现方案

  • vue全家桶

    vue-cli + vue2.0 + vuex + vue-route + axios + element-ui ...

  • VUE全家桶

    注意:这里只讲VUE脚手架搭建的项目首先安装开发环境,vue-cli 一键搭建vue项目是基于 Node.js 所...

网友评论

    本文标题:Vue全家桶多级嵌套

    本文链接:https://www.haomeiwen.com/subject/dfxfsqtx.html