我正在使用async/awaitTypeScript 进行一些基本的异步操作,但TSLint正在为下面这两个函数抛出神秘的错误消息.有没有人遇到过这些错误?在错误输出上没有提到管理规则,所以我不明白是什么导致这些.任何想法将不胜感激.
主要要求:
import * as rp from 'request-promise'
export function getRequest(address: rp.Options): rp.RequestPromise {
return rp(address)
}
Run Code Online (Sandbox Code Playgroud)
导出的异步功能:
export async function getStatus(message: Message) {
try {
const res = await getRequest(address)
if (res.ready) {
message.reply('...')
} else {
message.reply('...')
}
} catch (err) {
message.reply(err)
}
}
Run Code Online (Sandbox Code Playgroud)
这得到:Promises must be handled appropriately并且await of non-Promise对于第3行.
使用此导出的简单函数是:
client.on('message', message => {
if (message.content === 'green') {
getStatus(message)
}
})
Run Code Online (Sandbox Code Playgroud)
这也得到了Promises must be handled appropriately …
我正在尝试使用Bootstrap 4创建一个类似于屏幕截图的布局,但是我在修复侧边栏并同时实现此布局时遇到了一些问题.
以一个基本的例子为例:
<div class="container">
<div class="row">
<div class="col-m-4" id="sticky-sidebar">
Sidebar
</div>
<div class="col-m-8" id="main">
Main Area
</div>
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
我可以获得这种布局,但是一旦我声明,事情变得棘手:
.sidebar {
position: fixed // or absolute
}
Run Code Online (Sandbox Code Playgroud)
一旦我使侧边栏变粘,主 div开始出现在侧边栏后面而不是旁边.当然,可以宣布一些余量并将其推回原来的位置,但这会使响应性变得复杂.
我觉得我错过了什么,我读了Bootstrap 4文档,但我找不到一种简单的方法来实现这种布局.
想象一下,我有一个名为incomingValue的变量,我从API获取一个数字作为它的值.这些值在0到1之间,我正在设置另外两个变量,具体取决于这个值,使用一堆if语句,如下所示.
var incomingValue; // Set by an API
var setValueName;
var setValueIcon;
if (incomingValue < 0.10 ) {
setValueName = 'something';
setValueIcon = 'something.png'
}
if (incomingValue > 0.09 && incomingValue < 0.20 ) {
setValueName = 'somethingElse';
setValueIcon = 'somethingElse.png';
}
Run Code Online (Sandbox Code Playgroud)
在实际实现中,我有大约10个if语句检查特定时间间隔直到1.例如,如果它大于0.10但小于0.16,则执行此操作,依此类推.
作为一个JavaScript初学者,感觉这不是正确的做事方式,即使它完成了工作.我该如何重构这段代码?
更新:根据要求,我正在添加原始代码中使用的全套间隔.我之前没有包括完整列表,因为间隔不遵循某种模式.
我正在玩ES6类,我的最终目标是了解类与构造函数与工厂函数之间的区别。我目前的理解是,构造函数和类基本上使用相同的设计模式,而类只是构造函数的新语法。基于此假设,我试图创建一些示例,以显示类/构造函数与工厂函数之间的对比。
在下面的示例中,我旨在返回新对象和继承的一些方法。
我的第一个示例使用类语法非常简单。
示例#1:类
class Person {
constructor(name, age, location, occupation) {
this.name = name;
this.age = age;
this.location = location;
this.occupation = occupation;
}
printDescription() {
console.log(`My name is ${this.name} and I'm ${this.age} years old. I live in ${this.location} and I work as a ${this.occupation}.`);
}
}
const firstUser = new Person('Tom', 30, 'Sydney', 'Teacher');
firstUser.printDescription();
Run Code Online (Sandbox Code Playgroud)
在第二个示例中,我尝试使用不同的方法来复制第一个示例,但是我不确定这是工厂构造函数还是工厂函数。
实施例#2: ?
function PersonMaker (name, age, location, occupation) {
let person = {name, age, location, occupation};
person.printDetails = () …Run Code Online (Sandbox Code Playgroud) 我在 Node.js 应用程序前面使用 NGINX 作为反向代理。基本代理运行良好,我能够使用compression中间件压缩 Node 服务器上的资产。
为了测试是否可以将压缩任务委托给 NGINX,我禁用了中间件,现在我尝试使用以下配置使用 NGINX 进行 gzip:
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 300;
server {
listen 80;
## gzip config
gzip on;
gzip_min_length 1000;
gzip_comp_level 5;
gzip_proxied any;
gzip_vary on;
gzip_types text/plain
text/css
text/javascript
image/gif
image/png
image/jpeg
image/svg+xml
image/x-icon;
location / {
proxy_pass http://app:3000/;
proxy_http_version 1.1;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_cache_bypass …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用aurelia-router在Aurelia中配置路由,但是会引发一个错误,即找不到我的路由的moduleId(路由到文件)。
我在两个文件router.js和app.js中配置路由,路由器文件仅包含一个包含所有路由的数组。应用程序文件是我项目的主文件。
export default [
{ route: ['', '/', 'home'], name: 'home', title: 'Inicio', layoutView: 'components/common/layout/layout.html', moduleId: 'components/home/home' }
]
Run Code Online (Sandbox Code Playgroud)
import routes from 'router'
import {RouterConfiguration, Router} from 'aurelia-router';
export class App {
configureRouter(config, router){
config.options.root = '/';
config.title = 'La Tatuadora';
this.router = router;
config.map(routes);
}
}
Run Code Online (Sandbox Code Playgroud)
我正在使用 material-ui 日期选择器字段实现 redux-form。日期在字段中完美设置,但是当我尝试将其发送到日期的后端 API 格式时:
BeginDate_1: Tue Nov 14 2017 15:03:43 GMT+0530 (IST)
我正在尝试将此格式更改为“YYYY-mm-dd”格式,然后再将其发送到后端 API。
我尝试momentjs格式化,但无法得到我想要的结果。
这是我尝试过的:
主页.js
import React, {Component} from 'react';
import {Field, reduxForm} from 'redux-form';
import DatePicker from 'material-ui/DatePicker';
import {connect} from 'react-redux';
import * as moment from 'moment';
class Home extends Component {
renderCalendarField= ({
input,
label,
meta: {touched, error},
children,
...custom
}) => (
<DatePicker
floatingLabelText={label}
errorText={touched && error}
{...input}
value = {input.value !== ''? new Date(input.value) : null}
onChange={(event, value) => …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用Vue创建一个基本的选项卡式导航系统.我正在捕获用户的导航选择并调用一个更改某个数据的函数.该数据最终将决定哪个选项卡应该是活动的.到目前为止我有这个:
HTML
<div id="app">
<div id="nav">
<a href='#' class="tab" v-on:click="makeActive('homeActive')">Home</a>
<a href='#' class="tab" v-on:click="makeActive('infoActive')">Info</a>
</div>
<div class="content">
<div class="boxes" id="home" v-bind:class="choice">
</div>
<div class="boxes" id="info" v-bind:class="choice">
</div>
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
Vue公司
new Vue({
el: '#app',
data: {
choice: 'homeActive' // Home is chosen by default
},
methods: {
makeActive: function(val) {
this.choice = val;
}
}
});
Run Code Online (Sandbox Code Playgroud)
目前,如果用户点击Home链接,home和info div都会获得homeActive类,这是因为我无法通过这个基本逻辑使用我的方法返回两个语句:
启用tab1并禁用tab2 || 启用选项卡2并禁用tab1
我正在尝试不同的方法但由于将我的内容绑定到单个类,因此我只能通过调用方法影响单个状态.
关于如何使用Vue的任何建议?
在 web 上使用 React,很容易估计组件何时挂载或卸载,因为用户如何访问/离开某些路由。
如果我想在页面加载时获取一些数据并使用该数据动态显示组件,我会执行以下操作:
componentWillMount() {
this.props.fetchData();
}
Run Code Online (Sandbox Code Playgroud)
如果他们退出此页面并稍后回来,我知道他们回来了,所以我可以挂载并显示一些新数据。
但是,使用 React-Native for Android,有些我不明白。当用户通过按下主页按钮或概览按钮离开应用程序时,他们留下的组件不会卸载。因此,如果他们返回应用程序,组件将不会被挂载(它已经被挂载),因此,显示的数据不会被刷新。
如果用户使用“后退”按钮离开应用程序,这是个好消息,因为组件将卸载,因此当用户返回时,示例 fetchData 函数将再次触发。
使用“主页”和“概览”按钮跟踪用户行为的一般做法是什么?
我正在使用一个名为npm的软件包node-s3-client,该软件包是aws-sdkNode.js 的高级包装,用于将本地项目目录上载到S3存储桶。
使用包,我传递一些元数据到我的文件,即键值对Expires和Cache-Control。我正在上载包含HTML,JS,CSS,JPEG文件的整个目录。但是,当我检查我的S3存储桶时,我设置的标题仅适用于JS和CSS文件,而这些标题不适用于图像。
我已经阅读了软件包和aws-sdk的文档,但似乎找不到导致选择性地将我的元数据应用于某些文件而不应用于其他文件的原因。
这是我的配置对象:
const s3 = require('node-s3-client')
const s3Config= {
localDir: './dist',
deleteRemoved: false,
s3Params: {
Bucket: 'cdn',
Prefix: 'dist/',
Metadata: {
'Cache-Control': 'max-age=31536000',
'Expires': oneYearLater(new Date())
}
}
}
const client = s3.createClient({
s3Options: {
accessKeyId: KEY_ID,
secretAccessKey: ACCESS_KEY,
signatureVersion: 'v4',
region: 'us-east-2',
s3DisableBodySigning: true
}
})
client.uploadDir(s3Config)
Run Code Online (Sandbox Code Playgroud)
是什么导致此问题?
我最近从 TypeScript 2.1 升级,在所有较新的版本中,我的所有 React 组件都遵循示例中的模式,开始出现以下错误。在升级之前,这种嵌套组件模式不会导致任何问题。
组件#1:
interface ISpinnerProps{
showGlobalSpinner: boolean;
}
class SpinnerClass extends React.Component<ISpinnerProps, undefined> {
render() {
return (
// Some markup
)
}
}
export const Spinner = connect((state: State) => {
return {
showGlobalSpinner: selectors.showGlobalSpinner(state),
}
})(SpinnerClass)
Run Code Online (Sandbox Code Playgroud)
组件#2:
interface IProps {
loginStatus: fb.LoginStatus
}
class AppClass extends React.Component<IProps, undefined> {
render() {
return (
// Some markup
<Spinner/> // Error is thrown here.
)
}
}
export const App = connect(mapStateToProps, actionCreators)(AppClass)
Run Code Online (Sandbox Code Playgroud)
这会导致错误:
Type '{}' …
javascript ×7
node.js ×2
reactjs ×2
typescript ×2
amazon-s3 ×1
android ×1
asynchronous ×1
aurelia ×1
bootstrap-4 ×1
css ×1
date ×1
ecmascript-6 ×1
exception ×1
gzip ×1
html ×1
if-statement ×1
momentjs ×1
nginx ×1
react-native ×1
react-redux ×1
tslint ×1
vue.js ×1
vuejs2 ×1