node后台,MongoDB作為數據庫,vue前端獲取數據並渲染


 

前提:vue腳手架創建項目,node后台,MongoDB數據庫,並且跨域還有配置好

背景:前端Home.vue組件從后台獲取圖片並以輪播圖效果展示

 

后台:

models文件夾:存放各種數據庫文件,由於存在好多種數據表結構,連接數據庫時會產生問題,因此單獨創建一個文件夾,創建db.js文件,該文件專門連接數據庫。routes文件夾:路由處理文件

                                                                                                                 

 

 1 /** db.js
 2  * 完成 MongoDB 的連接,向外暴露一個連接成功的對象
 3  * @type {Mongoose}
 4  */
 5 
 6 module.exports = app =>{
 7     const mongoose = require('mongoose');
 8     mongoose.connect('mongodb://localhost: 27017/paper',{
 9         useNewUrlParser: true,
10         autoIndex: true
11     });
12 };

注意,db.js文件要在app.js中掛載一下,這樣不用在每個數據庫文件中引入

 1 // 引入數據庫連接文件 2 require('./util/db')(app); 

 1 /**
 2  * lunbotu.js
 3  *   輪播圖數據庫,存放輪播圖
 4  * @type {createApplication}
 5  */
 6 
 7 const mongoose = require('mongoose');
 8 
 9 const lunbotuSchema = new mongoose.Schema({
10     id:{
11         type:Number,
12         required:true
13     },
14     img_url:{
15         type:String,
16         required: true
17     }
18 });
19 
20 module.exports = mongoose.model('Lunbotu',lunbotuSchema);
 1 /**
 2  * Lunbotu.js
 3  *   處理輪播圖路由文件
 4  * @type {createApplication}
 5  */
 6 
 7 const express = require('express');
 8 
 9 const user = require('../models/lunbotu');
10 
11 const Lunbotu = express.Router();
12 
13 Lunbotu.get('/getlunbotu',async(req,res) =>{
14   const model = await user.find(req.body);
15   res.send(model)
16 });
17 
18 module.exports = Lunbotu;

當然,路由文件要在app.js中引入掛載一下

數據庫文件,先保存在json文件中,后插入數據庫:

 1 {
 2   "lunbotus": [
 3     {"id": 1,"img_url": "https://imgcps.jd.com/ling/32516359271/5ZOB54mM6ZKc5oOg/MuS7tjnmipg/p-5bd8253082acdd181d02f9e3/d9f21951.jpg"},
 4     {"id": 2,"img_url": "https://img13.360buyimg.com/pop/s590x470_jfs/t1/102707/14/10983/62930/5e225312E0f1eba30/bc6455afaf998638.jpg.webp"},
 5     {"id": 3,"img_url": "https://img30.360buyimg.com/pop/s590x470_jfs/t1/85339/25/10650/75930/5e200467E0b197a12/b8ace6cd19292e26.jpg.webp"},
 6     {"id": 4,"img_url": "https://img13.360buyimg.com/da/s590x470_jfs/t1/52408/25/3424/84234/5d123c03Ebc277535/416e2048cdc0e87f.jpg.webp"},
 7     {"id": 5,"img_url": "https://img14.360buyimg.com/pop/s590x470_jfs/t1/97733/27/10037/93770/5e159e3eEdc7a4241/7e038b38040faa38.jpg.webp"},
 8     {"id": 6,"img_url": "https://img12.360buyimg.com/pop/s590x470_jfs/t1/110249/27/4836/99962/5e268761E6c600e7d/d400bdc49862944a.jpg.webp"}
 9     ]
10 }

數據插入數據庫:

 

 

前端:

 1 <script>
 2     export default {
 3         name: "Home",
 4       data(){
 5           return{
 6             lunbotuList:[] // 獲取存放輪播圖
 7           }
 8       },
 9       created(){
10           this.getlunbo() // 調用方法
11       },
12       methods:{
13           async getlunbo(){
14             const res = await this.$axios.get('/getlunbotu',this.model);
15             // console.log(res);
16 if(res.status === 200 ){ //當狀態碼為200時表示請求成功 17 this.lunbotuList = res.data // 將數據保存在數組中 18 } 19 } 20 } 21 } 22 </script>

上述代碼中:console.log(res) 結果如下圖所示:

                                                         

 

 1 <template>
 2   <div>
 3 <!--    輪播圖區域 -->
 4 <!--    auto 毫秒值 -->
 5     <mt-swipe :auto="3000">
 6       <mt-swipe-item v-for="item in lunbotuList" :key="item.id">
 7         <img :src="item.img_url">
 8       </mt-swipe-item>
 9     </mt-swipe>
10 </div>
11 </template>


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM