小编Den*_*Hiu的帖子

PropTypes 在 Typescript React 中过时了吗?

我最近从基于 javascript 的 react 迁移到了 Typescript-react。所以这对我来说是新的。

我注意到 Typescript 使用interface. 我的问题是:

  1. 是否仍然需要使用 PropTypes(来自 prop-types 包)?
  2. 如何为道具制作“isRequired”?如果没有定义道具,我想让它出错。
  3. 如何为道具设置默认值?如果未定义道具,我想使用该默认值。

这是我的简单代码。以下代码有效:

import * as React from "react";

// [Question 1] Create an object PropTypes, is this still necessary ?
import * as PropTypes from "prop-types";

// [Question 2] how to use isRequired for this prop ?
interface Props {
  text: String;
  active: Boolean;
}

const NavBarMenu = (props: Props) => {
  // [Question 3] How to make a default value for prop? …
Run Code Online (Sandbox Code Playgroud)

javascript typescript reactjs

6
推荐指数
1
解决办法
5299
查看次数

如何使用重定向模式(无弹出窗口)从 Google Signin 获取个人资料信息?

在获取登录的客户端文件后,我是这样做的:

// HTML 
<script type='text/javascript' src="https://apis.google.com/js/api:client.js" async defer></script>
Run Code Online (Sandbox Code Playgroud)

我将gapi.load()函数调用到 HTML 按钮中

// load the startApp function after the page loads
jQuery(function () {
    $(window).load(function () {
       startApp()
    })
})


var startApp = function () {

    gapi.load('auth2', function () {

        // Retrieve the singleton for the GoogleAuth library and set up the client.
        auth2 = gapi.auth2.init({
            client_id: 'xxxxxxxx-xxxxxxxxxx.apps.googleusercontent.com',
            cookiepolicy: 'single_host_origin',
            ux_mode: 'redirect',      // I don't want it to display a pop-up 
            scope: 'profile email'    // I just need to …
Run Code Online (Sandbox Code Playgroud)

html javascript google-signin

6
推荐指数
1
解决办法
253
查看次数

如何在 Rust 中使用 Mongodb::cursor?

这是一个非常简单的项目,用于学习如何将 mongodb 与 Rust 结合使用。我在这里使用官方 mongodb 驱动程序:https://github.com/mongodb/mongo-rust-driver。问题是,如果我使用aggregate,我无法读取结果

// main.rs
use mongodb::bson::{doc, Bson};
use mongodb::{options::AggregateOptions, options::ClientOptions, Client};
use std::error::Error;
use tokio;

#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {

   // Load the MongoDB connection string:
   let client_uri = "mongodb://127.0.0.1:27017";

   // A Client is needed to connect to MongoDB:
   let mut options = ClientOptions::parse(&client_uri).await?;
   options.app_name = Some("testing".to_string());
   let client = Client::with_options(options)?;
   
   // get the collection here
   let items = client.database("my_database").collection("inventory");

   // aggregate options and pipeline
   let pipeline …
Run Code Online (Sandbox Code Playgroud)

mongodb rust

6
推荐指数
1
解决办法
3010
查看次数

如何从 Chrono::DateTime 获取年、月和日期组件?

该文档没有提及任何有关此主题的内容。我需要将其转换为 吗Date<Tz>?即使这样,也没有函数可以从中获取年份部分。

let current_date = chrono::Utc::now();
let year = current_date.year();  //this is not working, it should output the current year with i32/usize type
let month = current_date.month();
let date = current_date.date();
Run Code Online (Sandbox Code Playgroud)
let current_date = chrono::Utc::now();
let year = current_date.year();  //this is not working, it should output the current year with i32/usize type
let month = current_date.month();
let date = current_date.date();
Run Code Online (Sandbox Code Playgroud)

rust rust-chrono

6
推荐指数
1
解决办法
3524
查看次数

在同一台机器上添加辅助副本集?

我是 mongodb 的新手,所以这种复制对我来说有点困惑。我按照这里的教程进行操作。我在我的机器上设置了 2 个 mongod 实例:

localhost:27018
localhost:27019
Run Code Online (Sandbox Code Playgroud)

用这个命令:

mongod --dbpath /home/db2 --port 27019 --replSet "rs1"
mongod --dbpath /data/db1 --port 27018 --replSet "rs1"
Run Code Online (Sandbox Code Playgroud)

当我尝试使用这组命令连接并设置复制时

mongo --port 27019
rs1:PRIMARY> rs.add("localhost:27018")
Run Code Online (Sandbox Code Playgroud)

它总是说:

{
"ok" : 0,
"errmsg" : "Either all host names in a replica set configuration must
be localhost references, or none must be; found 1 out of 2",
"code" : 103
}
Run Code Online (Sandbox Code Playgroud)

有什么帮助吗?

mongodb

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

ngRepeat 中的自定义 $index 名称

我有一个用 JADE 编写的代码,它使用两个 ngRepeat 指令,如下所示:

table
    tr(ng-repeat="item in items track by $index")
       td: .dropdown
           button.btn.btn-default(data-toggle="dropdown") Choose One
           ul.dropdown-menu
               li(ng-repeat="option in item.options")
                  // I need this $index's value comes from the first ng-repeat above
                  a(ng-click="changeOption(item, option, $index)") 
Run Code Online (Sandbox Code Playgroud)

标签<a>有一个ng-click指令调用changeOption(item, option, $index)

问题是我需要$index从函数中的第一个ng-repeat指令changeOption()而不是第二个指令。我可以更改$index为另一个名称或将其分配给另一个变量,以便我可以在第二次迭代中访问它吗?

谢谢

javascript angularjs

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

如何在角度指令(transclude)中包含 &lt;tr&gt; 元素?

我想包括<tr>并且<td>显然我不能用指令来做到这一点。它一直忽略<td><td>好像它们不存在。这是我想要完成的:

<my-table>
   <tr>
     <td>hello</td>
     <td>world</td>
   </tr>
</my-table>
Run Code Online (Sandbox Code Playgroud)

这是javascript:

angular.module('app', [])
.controller('Controller', ['$scope', function($scope) {

}])
.directive('myTable', function() {
   return {
     restrict: 'E',
     transclude: true,
     templateUrl: 'my-table.html'
   };
});
Run Code Online (Sandbox Code Playgroud)

我的表.html:

<table ng-transclude>

</table>
Run Code Online (Sandbox Code Playgroud)

上面的代码导致:

<my-table class="ng-isolate-scope"><table ng-transclude="">

   hello  <-- no <tr> nor <td> here just plain text
   world

</table></my-table>
Run Code Online (Sandbox Code Playgroud)

示例:PLUNKR

html javascript angularjs

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

如何在 ImageBackground 组件(React Native)中调整背景位置?

我有一个这样的图像作为资产seats.png(180 x 36 像素),每个“座位”是一个 36x36 像素的图像:

这是我的头像

我的代码:

<View style={styles.container}>
      <ImageBackground 
      style={styles.redSeat}
      source={seatsPng}
      resizeMode="cover"
      >
      </ImageBackground>
</View>

import EStyleSheet from 'react-native-extended-stylesheet';

export default EStyleSheet.create({
  container: {
    backgroundColor: '#333',
    paddingTop: 10,
    paddingBottom: 10,
    paddingLeft: 10,
    paddingRight: 10,
  },
  redSeat: {
    width: 36, 
    height: 36,
    left: 0 // I want to show only red seat on the display
  }
});
Run Code Online (Sandbox Code Playgroud)

如何使它可以显示红色座位、黄色座位或紫色座位,只需调整图像偏移量,如 HTML/CSS 中的背景位置?上面的代码只显示一个绿色的座位。

PS:我搜索了 React-native GitHub 页面,我不敢相信这个问题被标记为关闭而开发人员没有解决方案。因此,如果将来可能有解决方案,我将打开此线程。https://github.com/facebook/react-native/issues/12347

react-native react-native-stylesheet

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

Cargo 包不适用于 Rust 的工作区?

我按照我在此处阅读的教程创建了一个“工作区”,其中包含多个文件夹

cargo run使用或成功运行cargo build

如果所有包都相互独立,cargo package则可以成功运行。但一旦一个包依赖于另一个包,就会cargo package失败。

它显示:no matching package named "foo_2" found. location searched: registry "crates-io"。这很奇怪,因为我专门在依赖项上添加了本地路径。

这是有意的行为吗?如果是这样,那么我为什么要为工作区烦恼呢?

根 Cargo.toml

[workspace]
members = [
    "foo_1",
    "foo_2",
]
Run Code Online (Sandbox Code Playgroud)

foo_1/Cargo.toml

[package]
name = "foo_1"
version = "0.1.0"
edition = "2021"

# error here. It can't found the foo_2 package.
[dependencies] 
foo_2 = { path = "../foo_2", version = "0.1.0" }
Run Code Online (Sandbox Code Playgroud)

foo_2/Cargo.toml

[package]
name = "foo_2"
version = "0.1.0"
edition = "2021"

[dependencies]
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

错误信息: …

rust rust-cargo

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

在Awesomium打印到打印机?

如何在Awesomium中处理window.print()事件到实际打印(不打印到PDF)?我知道它会使用WebView.PrintRequest事件,但我不知道该怎么做

http://docs.awesomium.net/1_7_0/html/E_Awesomium_Core_WebView_PrintRequest.htm

c# printing awesomium

4
推荐指数
1
解决办法
2173
查看次数