小编Nic*_*lli的帖子

如何避免 ReactJs 中的 CSS 冲突

我根本不是反应方面的专家,但从我可以看到,如果我导入一些CSS表,这些将适用于我的所有应用程序。如果我想将 React 用于多页面应用程序,如何为每个页面定义 css 表?

我的文件结构

第1页

import React, { Component } from "react";
import "./style.css";

export default class Page1 extends Component {
  render() {
    return (
      <div>
        <button>with css</button>
      </div>
    );
  }
}
Run Code Online (Sandbox Code Playgroud)

第2页

import React, { Component } from "react";

export default class Page2 extends Component {
  render() {
    return (
      <div>
        <button>no css</button>
      </div>
    );
  }
}
Run Code Online (Sandbox Code Playgroud)

样式.css

button {
  background: green;
}
Run Code Online (Sandbox Code Playgroud)

此样式只能应用于第一页,但也适用于第二页。我该如何解决这个问题?

html javascript css frontend reactjs

11
推荐指数
2
解决办法
1万
查看次数

我应该将所有快速服务器包装在带有打字稿的类中吗?

我认为自己对nodeJs足够称职.我最近决定通过开始使用Typescript来改变我的应用程序.我最近看到很多博客(比如这个),在创建RESTful API时,它们将所有模块包装在一个类中,最重要的是应用程序的入口点.它是正确的还是我可以像往常一样继续用打字稿开发我的应用程序?

javascript node.js express typescript

5
推荐指数
1
解决办法
317
查看次数

使用带有打字稿的猫鼬创建自定义验证时出错

 import mongoose, { Schema, model } from "mongoose";

 var breakfastSchema = new Schema({
      eggs: {
        type: Number,
        min: [6, "Too few eggs"],
        max: 12
      },
      bacon: {
        type: Number,
        required: [true, "Why no bacon?"]
      },
      drink: {
        type: String,
        enum: ["Coffee", "Tea"],
        required: function() {
          return this.bacon > 3;
        }
      }
    });
Run Code Online (Sandbox Code Playgroud)

运行此代码时遇到的两个错误是:

  • 类型 '{ type: StringConstructor; 上不存在属性 'bacon' 枚举:字符串[]; 要求:() => 任何;}'
  • 'required' 隐式具有返回类型 'any',因为它没有返回类型注释并且在其返回表达式之一中被直接或间接引用。

javascript mongoose mongodb typescript

5
推荐指数
1
解决办法
1395
查看次数

使用 typescript 创建 mongoose 插件

我在互联网上发现了这篇关于猫鼬插件的有趣文章。从那个网站我得到了这个代码:

    function HidePlugin(schema) {
  var toHide = [];
  schema.eachPath(function(pathname, schemaType) {
    if (schemaType.options && schemaType.options.hide) {
      toHide.push(pathname);
    }    
  });
  schema.options.toObject = schema.options.toObject || {};
  schema.options.toObject.transform = function(doc, ret) {
    // Loop over all fields to hide
    toHide.forEach(function(pathname) {
      // Break the path up by dots to find the actual
      // object to delete
      var sp = pathname.split('.');
      var obj = ret;
      for (var i = 0; i < sp.length - 1; ++i) {
        if (!obj) {
          return;
        } …
Run Code Online (Sandbox Code Playgroud)

plugins mongoose mongodb node.js typescript

5
推荐指数
0
解决办法
986
查看次数

仅从我的应用程序访问我的后端node.js

我想知道如何才能做到只有我的使用本机 React 开发的 Android 应用程序才能访问我的 API Node js。因此,我的服务器只能从我的网站(使用简单的域白名单)和我的应用程序访问

javascript whitelist node.js server react-native

3
推荐指数
1
解决办法
882
查看次数

UserModel不可分配给Document | Type类型的参数 nulll [带打字稿的猫鼬]

打字稿给我一个错误。该错误是由于猫鼬期望搜索的诺言将以猫鼬文档(在这种情况下为用户的文档)或“空”而解决的。但是我想访问所有用户的方法,因此我必须指定该函数将用户的模型作为参数。但是这件事导致我以下错误:

类型'(user:UserModel)=> Response'的参数不能分配给类型'(res:Document | null)=> void | 回应| PromiseLike'。

这是代码:

    import express, { Request, Response } from "express";


   import User, { UserModel } from "../../models/User";
    // router
    const router = express.Router();

    // @route   GET api/user/username/:username
    // @access  Public

    router.get("/username/:username", (req: Request, res: Response) => {
      User.findOne({ username: req.params.username })
        .then((user: UserModel | null) => res.json(user))
        .catch(err => res.json(err));
    });

    export default router;
Run Code Online (Sandbox Code Playgroud)

这是用户的模型:

import mongoose, { Schema, model } from "mongoose";

export type UserModel = mongoose.Document & {
  name: string; …
Run Code Online (Sandbox Code Playgroud)

javascript mongoose node.js express typescript

3
推荐指数
1
解决办法
516
查看次数

使用 TypeScript 配置 React Native 时出错

为了配置我的 React Native 项目,我仔细遵循了以下过程: https: //facebook.github.io/react-native/blog/2018/05/07/using-typescript-with-react-native。但是我要编译这个,我从打字稿编译器中得到了十五个错误。

以下是一些错误:

  • 无法重新声明块作用域变量“navigator”。
  • 后续的属性声明必须具有相同的类型。属性“geolocation”必须为“Geolocation”类型,但此处的类型为“GeolocationStatic”
  • 找不到名称“地图”。
  • 后续变量声明必须具有相同的类型
  • 重复的标识符“RequestInfo”。
  • 'FormData' 也在这里声明。
  • 无法重新声明块作用域变量“console”。
  • 不重新声明块作用域变量“navigator”。

信息:

"@types/react": "^16.7.3",
"@types/react-native": "^0.57.8",
"babel-jest": "23.6.0",
"jest": "23.6.0",
"metro-react-native-babel-preset": "0.49.1",
"react-native-typescript-transformer": "^1.2.10",
"react-test-renderer": "16.6.0-alpha.8af6728",
"typescript": "^3.1.6"
Run Code Online (Sandbox Code Playgroud)

更新

tsconfig.json

{
  "compilerOptions": {
    "target": "es2017",
    "module": "commonjs",
    "allowJs": true,
    "jsx": "react-native",
    "sourceMap": true,
    "outDir": "./build",
    "rootDir": "./src",
    "strict": true,
    "allowSyntheticDefaultImports": true,
    "esModuleInterop": true
  },
  "exclude": ["build", "index.js", "node_modules"]
}
Run Code Online (Sandbox Code Playgroud)

包.json

{
  "name": "test",
  "version": "0.0.1",
  "private": true,
  "scripts": {
    "start": "node node_modules/react-native/local-cli/cli.js start",
    "test": …
Run Code Online (Sandbox Code Playgroud)

node.js npm typescript reactjs react-native

3
推荐指数
1
解决办法
5554
查看次数