https://www.jianshu.com/p/ec436222c608
ps:這個簡單小項目只提供一個小小小的骨架,須要向「它」身上具體加多少「肉」,須要你們考慮好功能和佈局後進行完善。css
主頁分析:大致上分爲上(header)、中(body或content)、下(footer)三部分,中間body部分是由若干個相同的li組成的「列表」,因此咱們可將li定義爲一個組件。html
詳情頁分析:也分爲上、中、下三部分。vue
在src文件夾(也就是咱們碼農主要工做區)下,建立assets文件夾(用來保存項目所需圖片)、components(存組件commonFooter.vue、DetailHeader.vue、homeHeader.vue、list.vue)、pages(存頁面goodsDetail.vue、home.vue)和main.js文件。詳情請看下圖:node
ps: 1.具體文件夾以及文件名稱可根據本身項目進行「自擬」。 2.這裏的每個*.vue文件都是一個組件。webpack
主頁商品信息及圖片, 是從服務器端返回的json數據,不可能因此商品都「寫死」。故這裏須要模擬後臺創建了一個數據文件。在根目錄下創建一個goods.json文件用來放「僞數據」,以下圖:ios
注意json裏不能帶有註釋和其餘文字,否則會項目跑不起來,出錯git
{ "goods": [ { "price": "69.9", "title": "德芙", "img": "http://m.360buyimg.com/babel/s211x211_jfs/t3688/270/776223567/128582/fa074fb3/58170f6dN6b9a12bf.jpg!q50.jpg.webp" }, { "price": "63", "title": "費列羅", "img": "http://m.360buyimg.com/babel/s211x211_jfs/t613/100/1264998035/221234/1a29d51f/54c34525Nb4f6581c.jpg!q50.jpg.webp"}, { "price": "29.9", "title": "大米", "img": "http://m.360buyimg.com/babel/s211x211_jfs/t1258/40/17387560/108696/aced445f/54e011deN3ae867ae.jpg!q50.jpg.webp"}, { "price": "54.9", "title": "安慕希", "img": "http://m.360buyimg.com/babel/s211x211_jfs/t2734/15/680373407/215934/3abaa748/572057daNc09b5da7.jpg!q50.jpg.webp"}, { "price": "58", "title": "金典", "img": "http://m.360buyimg.com/babel/s211x211_jfs/t2482/145/1424008556/91991/d62f5454/569f47a2N3f763060.jpg!q50.jpg.webp"}, { "price": "60", "title": "味可滋", "img": "http://m.360buyimg.com/babel/s211x211_jfs/t2368/3/874563950/70786/7b5e8edd/563074c8N4d535db4.jpg!q50.jpg.webp" }, { "price": "248.00", "title": "瀘州老窖", "img": "http://m.360buyimg.com/babel/s211x211_jfs/t283/166/1424018055/189580/7c0792b7/543b4958N05fa2feb.jpg!q50.jpg.webp"}, { "price": "328.8", "title": "劍南春", "img": "http://m.360buyimg.com/babel/s350x350_g15/M05/1A/0A/rBEhWlNeLAwIAAAAAAHyok3PZY0AAMl8gO8My0AAfK6307.jpg!q50.jpg.webp"}, { "price": "49.00", "title": "藍莓", "img": "http://m.360buyimg.com/babel/s211x211_jfs/t2332/148/2952098628/94387/e64654e2/56f8d76aNb088c2ab.jpg!q50.jpg.webp" }, { "price": "68", "title": "芒果", "img": "http://m.360buyimg.com/n0/jfs/t3709/334/1378702984/206759/5c100ab5/58253588Naaa05c5c.jpg!q70.jpg"} ] }
使用node中的express,
安裝express和axiosgithub
在main.js裏配置axios到原型鏈中
注意標記的那兩行web
import Vue from 'vue' import App from './App' import router from './router' import axios from 'axios' //注意這行 Vue.prototype.$http = axios; //注意這行 Vue.config.productionTip = false new Vue({ el: '#app', router, components: { App }, template: '' })
在build>>webpack.dev.conf.js配置express並設置路由規則express
如圖:
代碼以下:
'use strict' const utils = require('./utils') const webpack = require('webpack') const config = require('../config') const merge = require('webpack-merge') const baseWebpackConfig = require('./webpack.base.conf') const HtmlWebpackPlugin = require('html-webpack-plugin') const FriendlyErrorsPlugin = require('friendly-errors-webpack-plugin') const portfinder = require('portfinder') /* datura_lj 增長express 20171126 */ const express = require('express') const app = express() var appData = require('../goods.json')/*加載本地數據文件*/ var goods = appData.goods var apiRoutes = express.Router() app.use('/api', apiRoutes) /* 增長express end */ const devWebpackConfig = merge(baseWebpackConfig, { module: { rules: utils.styleLoaders({ sourceMap: config.dev.cssSourceMap, usePostCSS: true }) }, // cheap-module-eval-source-map is faster for development devtool: config.dev.devtool, // these devServer options should be customized in /config/index.js devServer: { clientLogLevel: 'warning', historyApiFallback: true, hot: true, compress: true, host: process.env.HOST || config.dev.host, port: process.env.PORT || config.dev.port, open: config.dev.autoOpenBrowser, overlay: config.dev.errorOverlay ? { warnings: false, errors: true, } : false, publicPath: config.dev.assetsPublicPath, proxy: config.dev.proxyTable, quiet: true, // necessary for FriendlyErrorsPlugin watchOptions: { poll: config.dev.poll, }, /* datura_lj 增長express 20171126 */ before(app) { app.get('/api/goods', (req, res) => { res.json({ code: 0, data: goods }) }) } /* datura_lj 增長路由規則 end */ }, plugins: [ new webpack.DefinePlugin({ 'process.env': require('../config/dev.env') }), new webpack.HotModuleReplacementPlugin(), new webpack.NamedModulesPlugin(), // HMR shows correct file names in console on update. new webpack.NoEmitOnErrorsPlugin(), // https://github.com/ampedandwired/html-webpack-plugin new HtmlWebpackPlugin({ filename: 'index.html', template: 'index.html', inject: true }), ] }) module.exports = new Promise((resolve, reject) => { portfinder.basePort = process.env.PORT || config.dev.port portfinder.getPort((err, port) => { if (err) { reject(err) } else { // publish the new Port, necessary for e2e tests process.env.PORT = port // add port to devServer config devWebpackConfig.devServer.port = port // Add FriendlyErrorsPlugin devWebpackConfig.plugins.push(new FriendlyErrorsPlugin({ compilationSuccessInfo: { messages: [`Your application is running here: http://${config.dev.host}:${port}`], }, onErrors: config.dev.notifyOnErrors ? utils.createNotifierCallback() : undefined })) resolve(devWebpackConfig) } }) })
npm run dev 以後,測試是否該數據可用,在瀏覽器地址欄中輸入:http://localhost:8080/api/goods ,在瀏覽器中展現出以下圖數據,代碼數據數取成功:
配置config/index.js:解決跨域問題
在proxTable對象裏寫:
proxyTable: { '/api': { target: 'http://127.0.0.1:8080/api', changeOrigin: true, pathRewrite: { '^/api': '/' //寫'/api'就等於寫'http://127.0.0.1:8080/api' //到時候寫"/api/goods"就等於寫"http://127.0.0.1:8080/api/goods" } } }
原來參考教程有些地方須要修改:
home.vue裏的獲取數據要改爲這樣纔對:
2.修改list.vue裏的價格顯示那行代碼
3.在home.vue裏修改組件 : v-for="(item, index) in items" :key="index" 這樣就能夠運行了
期待擴寫功能:實現點擊商品列表的某一項(商品列表爲請求數據循環輸出),傳參到詳情頁,詳情頁根據傳過來的參數去請求數據回來填充到頁面.