前端学习笔记四十二-移动APP(6)douban电影项目

2020-04-02  本文已影响0人  AizawaSayo

fetch-jsonp插件跨域

1.安装
npm i fetch-jsonp -S

  1. 导入并和fetch一样使用
fetchJSONP(
      "https://api.douban.com/v2/movie/in_theaters?apikey=0b2bdeda43b5688921839c8ecb20399b"
    )
      .then(res => res.json())
      .then(data => {
        this.setState({
          isLoading: false
        })
        console.log(data)
      })

Node.js设置跨域


app.use('*', function (req, res, next) {

    // 设置请求头为允许跨域

    res.header("Access-Control-Allow-Origin", "*");

    // 设置服务器支持的所有头信息字段

    res.header("Access-Control-Allow-Headers", "Content-Type,Content-Length, Authorization, Accept,X-Requested-With");

    // 设置服务器支持的所有跨域请求的方法

    res.header("Access-Control-Allow-Methods", "POST,GET");

    // next()方法表示进入下一个路由

    next();

});

Promise规范

  1. 定义:就是一个异步的代码规范;

  2. 好处:

基于Promise规范的fetch API的使用

项目结构搭建和布局

  1. 运行npm install antd --save安装ant design

  2. 导入相关组件:


import { DatePicker } from 'antd';

  1. 导入样式:

import 'antd/dist/antd.css';

实现ANT组件的按需加载

  1. 运行cnpm i babel-plugin-import --save-dev

  2. 修改.babelrc文件:


{

    "presets":["es2015", "stage-0", "react"],

    "plugins":[

        "transform-runtime",

        ["import", { "libraryName": "antd", "style": "css" }]

        ]

}

  1. 然后只需从 antd 引入模块即可,无需单独引入样式。等同于下面手动引入的方式。

使用react-router-dom实现路由跳转


// 其中path指定了路由匹配规则,component指定了当前规则所对应的组件

<Route path="" component={}></Route>


    // 导入路由组件

    import {Route, Link, Redirect} from 'react-router-dom'



    <Redirect to="/movie/in_theaters"></Redirect>

this.prop和Route的关系【重要】

获取到参数之后,从服务器获取电影数据

使用Node服务器转接豆瓣API

渲染电影列表

相关文章

上一篇 下一篇

猜你喜欢

热点阅读