博客
关于我
nuxt全栈仿美团官网10——目录
阅读量:112 次
发布时间:2019-02-26

本文共 1140 字,大约阅读时间需要 3 分钟。

建立目录数据模型并完成数据库导入

在完成教材数据的导入后,我们需要设计数据模型结构,并将数据导入数据库。以下是具体的操作步骤:

  • 在server目录下创建dbs_copy目录,将教材中的数据库文件*.dat全部复制进去。

  • 配置MongoDB环境变量。在系统变量的path中添加MongoDB安装的bin目录,然后在dbs_copy目录下打开命令行窗口(如Git Bash),执行以下命令将menus集合导入student数据库:

    mongoimport -d student -c menus menus.dat

    使用相同的方法导入其他数据文件。

  • 接下来,我们需要在数据库中创建对应的数据模型。以下是使用Mongoose的示例代码:

    import mongoose from 'mongoose';const menuSchema = mongoose.Schema({  menu: {    type: Array,    required: true  }});export default mongoose.model('menu', menuSchema);

    完成数据库模型设计后,下一步是开发获取目录数据的接口。建议在geo.js中增加相应的RESTful API,并进行测试以确保数据能够正确查询和返回。

    为了方便管理目录数据,我们建议在store中创建对应的状态管理。以下是一个典型的 Vuex store 实现示例:

    const state = {  menu: []};const mutations = {  setMenu(state, menu) {    state.menu = menu;  }};const actions = {  setMenu({ commit }, menu) {    commit('setMenu', menu);  }};export default {  namespaced: true,  state,  mutations,  actions};

    最后,我们需要在Nuxt项目的nuxtServerInit方法中进行store的初始化。例如,在store.js中添加以下代码:

    import { mapGetters } from 'vuex';export default {  namespaced: true,  state: {    home: {      menu: []    }  },  mutations,  actions};

    在组件中使用store数据时,可以采用以下方式:

    通过以上步骤,我们可以完成目录数据的整体管理和展示。

    转载地址:http://azoy.baihongyu.com/

    你可能感兴趣的文章
    PostgreSQL cube 插件 - 多维空间对象
    查看>>
    PostgreSQL Daily Maintenance - cluster table
    查看>>
    PostgreSQL on Linux 最佳部署手册
    查看>>
    PostgreSQL Oracle 兼容性之 - pipelined
    查看>>
    PostgreSQL Point-In-Time Recovery (Incremental Backup)
    查看>>
    postgresql Streaming Replication监控与注意事项
    查看>>
    postgresql 不需要付费_使用数据传输在PostgreSQL执行 外部连接运算符
    查看>>
    postgresql 主从配置_生产环境postgresql主从环境配置
    查看>>
    postgresql 函数&存储过程 ; 递归查询
    查看>>
    PostgreSQL 分组聚合查询中 filter 子句替换 case when
    查看>>
    PostgreSQL 同步流复制锁瓶颈分析
    查看>>
    PostgreSQL 备份与还原命令 pg_dump
    查看>>
    Postgresql 外部表插件postgres_fdw的安装和使用
    查看>>
    PostgreSQL 如何从崩溃状态恢复(上)
    查看>>
    PostgreSQL 存储过程基本语法
    查看>>
    PostgreSQL 实现批量更新、删除、插入
    查看>>
    PostgreSQL 导入 .gz 备份文件
    查看>>
    PostgreSQL 批量插入&更新数据时报错(ERROR: ON CONFLICT DO UPDATE command cannot affect row a second time)
    查看>>
    PostgreSQL 新增数据返回自增ID
    查看>>
    postgresql 更新多列数据
    查看>>