我根本不是反应方面的专家,但从我可以看到,如果我导入一些CSS表,这些将适用于我的所有应用程序。如果我想将 React 用于多页面应用程序,如何为每个页面定义 css 表?
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)
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)
button {
background: green;
}
Run Code Online (Sandbox Code Playgroud)
此样式只能应用于第一页,但也适用于第二页。我该如何解决这个问题?
我认为自己对nodeJs足够称职.我最近决定通过开始使用Typescript来改变我的应用程序.我最近看到很多博客(比如这个),在创建RESTful API时,它们将所有模块包装在一个类中,最重要的是应用程序的入口点.它是正确的还是我可以像往常一样继续用打字稿开发我的应用程序?
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)
运行此代码时遇到的两个错误是:
我在互联网上发现了这篇关于猫鼬插件的有趣文章。从那个网站我得到了这个代码:
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) 我想知道如何才能做到只有我的使用本机 React 开发的 Android 应用程序才能访问我的 API Node js。因此,我的服务器只能从我的网站(使用简单的域白名单)和我的应用程序访问
打字稿给我一个错误。该错误是由于猫鼬期望搜索的诺言将以猫鼬文档(在这种情况下为用户的文档)或“空”而解决的。但是我想访问所有用户的方法,因此我必须指定该函数将用户的模型作为参数。但是这件事导致我以下错误:
类型'(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) 为了配置我的 React Native 项目,我仔细遵循了以下过程: https: //facebook.github.io/react-native/blog/2018/05/07/using-typescript-with-react-native。但是我要编译这个,我从打字稿编译器中得到了十五个错误。
以下是一些错误:
信息:
"@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) javascript ×5
node.js ×5
typescript ×5
mongoose ×3
express ×2
mongodb ×2
react-native ×2
reactjs ×2
css ×1
frontend ×1
html ×1
npm ×1
plugins ×1
server ×1
whitelist ×1