小编kka*_*gil的帖子

如何在nextjs中获取上一个网址

如何在nextjs中获取以前的网址?我认为 values ,this.props.router.asPath并且nextProps.router.asPath是不同的。

其实我想router.push登录后。我知道router.back去上一页。但是可以去其他网站。有历史栈的用户到上一页,没有历史栈的用户到/主页面。

import { Component } from 'react'
import App, { Container } from 'next/app';
import ErrorComponent from '@/components/error'

export default class MyApp extends App {
  render() {
    console.log(this.props)
    const { Component, pageProps, router } = this.props;
    const props = {
      ...pageProps,
      router
    }
    return (
      <ErrorBoundary>
        <Container>
          <Component {...props} />
        </Container>
      </ErrorBoundary>
    );
  }

  componentWillReceiveProps(nextProps) {
    // previous page url /contents
    console.log(this.props.router.asPath) // /about
    console.log(nextProps.router.asPath) // /about …
Run Code Online (Sandbox Code Playgroud)

next.js

10
推荐指数
3
解决办法
2万
查看次数

Mobx 控制台警告

我收到了来自 Mobx 的警告信息。

[mobx.array] 尝试读取超出范围 (0) 的数组索引 (0)。请先检查长度。MobX 不会跟踪越界索引

@observable checks = {
      deviceType: ['phone','laptop', ...],
      deviceTypeChecks: [],
      ...
    }

@action
selectAllChecks = (target, type) => {
     const targetChecks = []
     if (this.checks[target].length !== this.checks[type].length) {
        this.checks[target].forEach(el => targetChecks.push(el))
      }
     this.checks[type] = targetChecks
}
Run Code Online (Sandbox Code Playgroud)

我怎样才能删除那个警告?但是,这段代码没有问题。它运作良好。

我正在selectAllChecks通过 onChange 函数使用函数。

const {
  deviceType,
  deviceTypeChecks
} = this.props.store.checks

<label className="mr10">
          <input
            type="checkbox"
            checked={deviceType.length === deviceTypeChecks.length}
            onChange={() =>
              selectAllChecks('deviceType', 'deviceTypeChecks')
            }
          />
          <span>All device type</span>
        </label>
Run Code Online (Sandbox Code Playgroud)

我必须为 IE 提供 4 个版本。

"mobx": …
Run Code Online (Sandbox Code Playgroud)

reactjs mobx mobx-react

7
推荐指数
3
解决办法
8220
查看次数

如何在中间件中获取响应体?

我想用响应信息记录每个请求。

我尝试使用中间件,但遇到了问题。

res.body 未定义。

app.use((req, res, next) => {

    var time = Date.now();

    res.on('finish', function() {
        var clientIp = req.headers['x-forwarded-for'] || req.connection.remoteAddress;
        var method = req.method;
        var path = req.baseUrl;
        var duration = Date.now() - time;

        console.log({
            clientIp,
            elapsedTime: `${duration}ms`,
            hostname: req.headers.host,
            level: 'INFO',
            method,
            path,
            phase: process.env.NODE_ENV,
            reqBody: req.body,
            reqHeaders: req.headers,
            resBody: res.body,
            resHeaders: res.getHeaders(),
            status: res.statusCode
        });
    });

    next();
});
Run Code Online (Sandbox Code Playgroud)

实际上客户端上有响应数据。

如何在中间件中获得响应体?

node.js express

6
推荐指数
2
解决办法
2595
查看次数

我不了解对象内的传播语法

我不了解对象内的传播语法。

console.log(...false) // TypeError not iterable
console.log(...1) // TypeError not iterable
console.log(...null) // TypeError not iterable
console.log(...undefined) // TypeError not iterable
Run Code Online (Sandbox Code Playgroud)

我理解上面的代码由于非迭代器而发生错误。

但是这些代码运行良好。

console.log({...false}) // {}
console.log({...1}) // {}
console.log({...null}) // {}
console.log({...undefined}) // {}
Run Code Online (Sandbox Code Playgroud)

请让我知道为什么上述代码有效。

javascript spread-syntax

4
推荐指数
2
解决办法
1242
查看次数

CSSProperties TextAlignProperty 类型错误

我正在使用 React 16.8 和 Typescript "@zeit/next-typescript": "1.1.0"

我有内联风格的界面。并且有使用IInputInlineStyle接口的对象。

interface IInputInlineStyle {
    padding: string;
    textAlign?: string;
    paddingLeft?: string | number;
    color?: string;
}

 const inputInlineStyle: IInputInlineStyle = {
            padding,
            textAlign
        };

Run Code Online (Sandbox Code Playgroud)

但是我收到了这个错误信息。

错误:(155, 21) TS2322:类型“IInputInlineStyle”不可分配给类型“CSSProperties”。

属性“textAlign”的类型不兼容。

类型 'string' 不可分配给类型 'TextAlignProperty'。

我该如何解决这个问题?

typescript reactjs

2
推荐指数
2
解决办法
1815
查看次数