Git Product home page Git Product logo

doubanmovie's Introduction

豆瓣电影简单展示

this is doubanMovie show By vue2.0

演示地址

豆瓣电影 服务器欠费了,没有继续续费了,想要看效果的小伙伴可以fork下来在本地run dev 一下。

安装步骤

# install dependencies
npm install

# serve with hot reload at localhost:8080
npm run dev

# build for production with minification
npm run build

# build for production and view the bundle analyzer report
npm run build --report

使用技术简介 (vue2.x + webpack2.x + vue-resource + vue-router + vuex + Element-UI)

项目简单演示

img

教程

安装 vue-cli 脚手架

运行如下命令,即可创建一个名为 doubanMovie 的 vue 项目,并且通过本地 8080 端口启动服务

npm install -g vue-cli
vue init webpack doubanMovie
cd doubanMovie
npm install
npm run dev

在运行 vue init webpack doubanMovie 后,会依次要求输入以下配置内容

  • 项目名称
  • 项目描述
  • 作者
  • 选择 Vue 构建:运行+编译 或 仅运行时
  • 是否安装 vue-loader
  • 是否使用 ESLint
    • 如果是,请选择模式:标准模式、AirBNB 模式、自定义
  • 是否使用 Karma + Mocha 的单元测试
  • 是否使用 Nightwatch e2e 测试

安装完成后,即可看到以下文件结构:

.
|-- build                            // 项目构建相关代码
|   |-- build.js                     // 生产环境构建代码
|   |-- check-version.js             // 检查 node、npm 等版本
|   |-- dev-client.js                // 热重载相关
|   |-- dev-server.js                // 构建本地服务器
|   |-- utils.js                     // 构建工具相关
|   |-- webpack.base.conf.js         // webpack 基础配置(出入口和 loader)
|   |-- webpack.dev.conf.js          // webpack 开发环境配置
|   |-- webpack.prod.conf.js         // webpack 生产环境配置
|-- config                           // 项目开发环境配置
|   |-- dev.env.js                   // 开发环境变量
|   |-- index.js                     // 项目一些配置变量(开发环境接口转发将在此配置)
|   |-- prod.env.js                  // 生产环境变量
|   |-- test.env.js                  // 测试环境变量
|-- src                              // 源码目录
|   |-- components                   // vue 公共组件
|   |-- App.vue                      // 页面入口文件
|   |-- main.js                      // 程序入口文件,加载各种公共组件
|-- static                           // 静态文件,比如一些图片,json数据等
|-- test                             // 自动化测试相关文件
|-- .babelrc                         // ES6语法编译配置
|-- .editorconfig                    // 定义代码格式
|-- .eslintignore                    // ESLint 检查忽略的文件
|-- .eslistrc.js                     // ESLint 文件,如需更改规则则在此文件添加
|-- .gitignore                       // git 上传需要忽略的文件
|-- README.md                        // 项目说明
|-- index.html                       // 入口页面
|-- package.json                     // 项目基本信息
.

ESLint 配置

ESLint 配置在根目录的 .eslintrc.js 里。
正常情况下,ESLint 报错是因为你的代码不符合现有的 ESLint 规范。当然在开发的过程中,ESlint能够很好地规范你的代码,对于新手来说可能有点别扭,但是习惯了可以极大地提升代码的可读性。

静态页面开发  

此时,浏览器应该已经打开了 localhost:8080 页面。

在此情况下,请尝试更改 /src/App.vue/src/components/Hello.vue 文件中<template>标签内的内容,保存后即可立即看到浏览器页面已自动更新了你做出的改动。  这是因为我们配置了vue的热更新组件。在build目录的dev-client.js

/* eslint-disable */
require('eventsource-polyfill')
var hotClient = require('webpack-hot-middleware/client?noInfo=true&reload=true')

hotClient.subscribe(function (event) {
  if (event.action === 'reload') {
    window.location.reload()
  }
})

提示:./src/components 文件夹多用于保存公用组件。至于页面组件.

Element-ui

使用本项目使用的是Element-ui做为公共组件库,element-ui是由饿了么开源的基于vue2.0的组件库。具体可以参考官网element-ui

vue-router 2 使用

当一个个静态组件完成后,需要按照路由组织这些组件文件。

请前往 vue-router 2 介绍 阅读 基础 部分教程,并可以边阅读边配置路由。

路由文件是 ./src/router.index.js

本项目中使用了 HTML5 History 模式,路由配置比较简单,可以参考。

API 请求转发配置

至此,你应该已经完成了所有的静态页面的工作,接下来我们准备搭建请求,为后面的 xhr 请求做好准备。

  1. 打开 http://api.douban.com/v2/movie/in_theaters 查看接口数据,留意此地址。

  2. ./config/index.js 中的 proxyTable 配置代理:

    proxyTable: {
        '/api': {
            target: 'http://api.douban.com/v2',
            changeOrigin: true,
            pathRewrite: {
                '^/api': ''
            }
        }
    }
  3. 重新启动 npm run dev,打开 localhost:8080/api/movie/in_theaters 查看结果是否与直接请求豆瓣 API 相同。

  4. 本应该使用了以下 API:

    • /v2/movie/search?q={text} 电影搜索api;
    • /v2/movie/in_theaters 正在上映的电影;
    • /v2/movie/coming_soon 即将上映的电影;
    • /v2/movie/subject/:id 单个电影条目信息。

更多请参考 豆瓣电影 API 文档。

这样我们就可以在应用中调用 /api/movie/in_theaters 来访问 http://api.douban.com/v2/movie/in_theaters,从而解决跨域的问题。

build生产环境服务跨域方案

以nginx作为简单的描述:找到nginx的配置文件config/nginx.config;用下面代码替换这个文件里面的代码

#
# The default server
#

server {
    listen       80;

    # 需要将主机名改为豆瓣 api
    server_name  api.douban.com;

    # root 即指向服务器存放的编译出的 index.html 目录
    root         /var/www/Vdo/dist;

    # 设定根路由
    location / {

        # 指定 index 文件
        index index.html;

        # 将其他任何路由都交给 index.html 处理,保证能使用 HTML5 History 模式
        # REF https://router.vuejs.org/zh-cn/essentials/history-mode.html#nginx
        try_files $uri $uri/ /index.html;
    }

    # 设定转发豆瓣 api,即 localhost/api/XXX -> api.douban.com/v2/XXX
    location /api/ {

        # 设定头部
        proxy_set_header Host api.douban.com;

        # 设定代理目标
        proxy_pass http://api.douban.com/v2/;
    }
    error_page 404 /404.html;
        location = /40x.html {
    }

    error_page 500 502 503 504 /50x.html;
        location = /50x.html {
    }

}

保存启动ngix便可以了

使用 vue-resource

vue-resource 库使用起来相当方便。  

你可以在单个组件中尝试引入并调用:

import Vue from 'vue'
import $http from 'vue-resource'
Vue.use($http)

let vm = new Vue()

const configPath = '/api'
window.configPath = configPath

export class Utils {
  get (url, data = {}) {
    url = configPath + url
    return new Promise((resolve, reject) => {
      vm.$http.get(url, {params: data, credentials: true}).then((response) => {
        resolve(response.body)
      }, function () {
        console.log('接口异常')
      })
    })
  }
}

这里,只使用了里面的get方法

更多 vue-resource 用法请参考 vue-resource

使用 Vuex 并分离代码

为了试代码更加结构化,我们应当将数据请求和视图分离。

这一节中,我们有两个任务要做:

  1. 分离数据请求层逻辑。
  2. 使用 Vuex 管理状态。

将二者放到同一节中主要是因为二者再同一目录下,我们来查看 ./store 文件夹的结构:

.
|-- store                          // 数据处理根目录
|   |-- moving                     // 电影模块文件夹
|   |   |-- actions.js             // 电影模块获取接口actions
|   |   |-- getters.js             // Vuex getters
|   |   |-- index.js               // 定义store 模块入口文件
|   |   |-- mutations.js           // Vuex mutations
|   |   |-- type.js                // Vuex 操作 key
|   |-- index.js                   // Vuex 入口
.

我们需要先了解 Vuex。

请查看 Vuex 文档,了解其 核心概念

Vuex 是一个专为 Vue.js 应用程序开发的状态管理模式。它采用集中式存储管理应用的所有组件的状态,并以相应的规则保证状态以一种可预测的方式发生变化。Vuex 也集成到 Vue 的官方调试工具 devtools extension,提供了诸如零配置的 time-travel 调试、状态快照导入导出等高级调试功能。

其实在我看来,Vuex 相当于某种意义上设置了读写权限的全局变量,将数据保存保存到该“全局变量”下,并通过一定的方法去读写数据。(希望这能帮助你理解 Vuex)

结语

至此,主体工作已经完成。

欢迎 Star 本项目。

doubanmovie's People

Contributors

muwoo avatar silence-j avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

doubanmovie's Issues

Cannot read property 'Symbol(Symbol.iterator)' of undefined

code: https://github.com/kenpeter/myDoubanMovie
visit: http://doubanmov.shopshop.space

error:

vue.esm.js:1447 TypeError: Cannot read property 'Symbol(Symbol.iterator)' of undefined
    at movingList (http://doubanmov.shopshop.space/static/js/app.fb445042ed21485b7632.js:1:4428)
    at t._wrappedGetters.(anonymous function) (http://doubanmov.shopshop.space/static/js/vendor.1160383a5c83b579ed2c.js:11:34287)
    at oe.s.(anonymous function) (http://doubanmov.shopshop.space/static/js/vendor.1160383a5c83b579ed2c.js:11:32153)
    at yi.get (http://doubanmov.shopshop.space/static/js/vendor.1160383a5c83b579ed2c.js:6:53211)
    at yi.evaluate (http://doubanmov.shopshop.space/static/js/vendor.1160383a5c83b579ed2c.js:6:54112)
    at oe.movingList (http://doubanmov.shopshop.space/static/js/vendor.1160383a5c83b579ed2c.js:6:10552)
    at Object.get [as movingList] (http://doubanmov.shopshop.space/static/js/vendor.1160383a5c83b579ed2c.js:11:32221)
    at s.movingList (http://doubanmov.shopshop.space/static/js/app.fb445042ed21485b7632.js:1:9871)
    at yi.get (http://doubanmov.shopshop.space/static/js/vendor.1160383a5c83b579ed2c.js:6:53211)
    at yi.evaluate (http://doubanmov.shopshop.space/static/js/vendor.1160383a5c83b579ed2c.js:6:54112)

点个赞

说实话,这是一个非常好的实战项目,作者很严谨。

好像searchList.vue中的mounted()的作用不大??

将searchList.vue中的mounted()删除后,还是可以获取搜索的结果,请问此处的mounted的作用是什么,为什么header.vue中的searchMovie方法已使用了this.$store.commit('SEARCH_TEXT', {searchText: this.content})
和 this.$store.dispatch('getSearchList'),searchList.vue中的mounted()还用再次使用这2个,谢谢。

严格模式下报错

vuex在严格模式下报错 是直接修改了状态的属性引用而没去提交mutation

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.