小编Ron*_*nin的帖子

获取Angular中的当前URL

如何在AngularJS 4中获取当前URL?我在网上搜索了很多,但无法找到解决方案.

app.module.ts

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { RouterModule, Router } from '@angular/Router';

import { AppComponent } from './app.component';
import { TestComponent } from './test/test.component';
import { OtherComponent } from './other/other.component';
import { UnitComponent } from './unit/unit.component';

@NgModule ({
  declarations: [
    AppComponent,
    TestComponent,
    OtherComponent,
    UnitComponent
  ],

  imports: [
    BrowserModule,
    RouterModule.forRoot([
        {
            path: 'test',
            component: TestComponent
        },
        {
            path: 'unit',
            component: UnitComponent
        },
        {
            path: 'other',
            component: OtherComponent
        }
    ]),
  ],

  providers: …
Run Code Online (Sandbox Code Playgroud)

javascript url-routing typescript angular

101
推荐指数
3
解决办法
22万
查看次数

将JSON传递给HTTP POST请求

我正在尝试使用nodejs请求 [2] 向谷歌QPX Express API [1]发出HTTP POST请求.

我的代码如下:

    // create http request client to consume the QPX API
    var request = require("request")

    // JSON to be passed to the QPX Express API
    var requestData = {
        "request": {
            "slice": [
                {
                    "origin": "ZRH",
                    "destination": "DUS",
                    "date": "2014-12-02"
                }
            ],
            "passengers": {
                "adultCount": 1,
                "infantInLapCount": 0,
                "infantInSeatCount": 0,
                "childCount": 0,
                "seniorCount": 0
            },
            "solutions": 2,
            "refundable": false
        }
    }

    // QPX REST API URL (I censored my api …
Run Code Online (Sandbox Code Playgroud)

json curl node.js express node-request

84
推荐指数
4
解决办法
20万
查看次数

参数隐式具有"任何"类型

我正在使用visual studio代码进行打字稿项目,在那里我使用了一些第三方npm js库.其中一些不提供任何ts类型(types.d.ts文件),因此每当我使用参数或变量而不指定其类型时,vs代码的linting显示此错误:参数隐式具有"any"类型. 此外,ts不会编译.

我怎样才能防止这种情况发生?

typescript visual-studio-code typescript-typings

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

使用RxJs WebSocketSubject和Angular Universal时,"ReferenceError:未定义WebSocket"

我正在设置一个角度6.x通用项目,以利用其SSR(服务器端呈现)功能.在我的应用程序中,我正在使用RxJs进行websocket通信.

更具体地讲,我是,使用WebSocketSubjectwebSocket在我的角度普遍6.x的项目,该项目在浏览器平台上正常工作.但是,在运行节点Web服务器(包含SSR内容(服务器端呈现))时,会引发错误:

ReferenceError:未定义WebSocket

示例代码:

// not actually code from the reproduction repo
import { WebSocketSubject, webSocket } from 'rxjs/webSocket';

const socket: WebSocketSubject<any> = webSocket('wss://echo.websocket.org');
socket.subscribe(msg => doSomething(msg));
Run Code Online (Sandbox Code Playgroud)

请注意,应用程序的浏览器版本不会出现错误(即ng serve不会抛出错误),但只有在编译SSR内容并运行快速Web服务器之后.要重现错误,必须首先运行构建:

# install dependencies
npm install

# build angular bundles for browser and server platforms
npm run build:client-and-server-bundles

# build the webserver
npm run webpack:server

# start the webserver
npm run serve:ssr

# the app is now being served at http://localhost:8081/
# …
Run Code Online (Sandbox Code Playgroud)

node.js webpack angular-universal angular

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

找不到名称“描述”。您是否需要为测试运行程序安装类型定义?

当将TypeScript与Jest结合使用时,我的规格将因以下错误消息而失败:

test/unit/some.spec.ts:1:1 - error TS2582: Cannot find name 'describe'. Do you need to install type definitions for a test runner? Try `npm i @types/jest` or `npm i @types/mocha`.
test/unit/some.spec.ts:2:3 - error TS2582: Cannot find name 'it'. Do you need to install type definitions for a test runner? Try `npm i @types/jest` or `npm i @types/mocha`.
test/unit/some.spec.ts:3:7 - error TS2304: Cannot find name 'expect'.
test/unit/some.spec.ts:7:1 - error TS2582: Cannot find name 'test'. Do you need to install type definitions for a …
Run Code Online (Sandbox Code Playgroud)

typescript jestjs

8
推荐指数
17
解决办法
1万
查看次数

在 heroku 上部署 node.js 应用程序成功但不起作用

我有一个非常简单、直接的 node.js 应用程序 [1] 我想在 heroku 上部署。虽然我能够部署它,但无法在浏览器中访问该页面。

我遵循了“Heroku 上的 Node.js 入门”指南 [2] 中的建议。当我使用node index.js本地运行应用程序时,我可以在 上访问该应用程序http://localhost:8080/index.jade,但是,当我尝试在 heroku 上访问它时,http://immonow.herokuapp.com:8080/index.jade它会抛出一个ERR_CONNECTION_REFUSEDHTTP 错误代码。

我如何部署我的应用程序:

  1. git commit -am "made changes" // 提交更改
  2. git push origin master // 推送到 git
  3. heroku create // 创建 Heroku 应用程序
  4. git push heroku master // 推送到heroku
  5. heroku ps:scale web=1 // 启动工人

我的 node.js 服务器:

#!/usr/bin/env node
var http = require('http')
  , jade = require('jade')
  , static = require('node-static')
  , jadeRe = /\.jade$/
  , …
Run Code Online (Sandbox Code Playgroud)

git heroku node.js

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

无法将git存储库部署到dokku实例(致命:{reponame}似乎不是git存储库)

我正在尝试将我的本地git repo上的应用程序部署到digitalocean上的dokku实例.

区域文件

$ORIGIN mydomain.com.
$TTL 1800
...
mydomain.com. 1800 IN A 11.22.33.44
www.mydomain.com. 1800 IN CNAME mydomain.com.
*.mydomain.com. 1800 IN A 11.22.33.44
test.mydomain.com. 1800 IN A 11.22.33.44
Run Code Online (Sandbox Code Playgroud)

我打算部署我的应用程序 test.mydomain.com

dokku设置

我使用digitalocean上的dokku映像来设置服务器,该服务器目前在dokku上运行v0.4.15,并尝试根据显然过时的指南部署我的应用程序,但是,我没有找到更新的.

ssh设置

我还设置了SSH密钥和配置文件,因此我可以通过SSH登录我的dokku实例,用户rootdokku:

ssh {username}@test.localhome.ch // works

本地ssh配置文件

Host          mydomain.com test.mydomain.com
        HostName       11.22.33.44
        IdentityFile   ~/.ssh/mydomain
        PreferredAuthentications publickey
Run Code Online (Sandbox Code Playgroud)

部署

当我尝试部署我的git repo时:

git remote add dokku dokku@test.mydomain.com:test
git push dokku master
Run Code Online (Sandbox Code Playgroud)

它会扔给我

fatal: 'test' does not appear to be a git repository
fatal: …
Run Code Online (Sandbox Code Playgroud)

git ssh digital-ocean dokku

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

nginx 响应“301 永久移动”

考虑以下 nginx 配置文件:

server {
    listen 443;
    ssl    on;
    ssl_certificate     /etc/tls/cert.pem;
    ssl_certificate_key /etc/tls/key.pem;
    location / {
        proxy_pass http://api.default.svc.cluster.local;
    }
}
Run Code Online (Sandbox Code Playgroud)

443 上的所有传入 TCP 请求都应重定向到我运行的服务器api.default.svc.cluster.local:80(顺便说一句,这是一个 REST-Api 节点)。这工作正常,我可以curl https://<nginx-IP>/nginx 并获得正确的响应,如预期的那样。

现在,我想将位置从 更改为//api这样我就可以触发 acurl https://<nginx-IP>/api以获得与以前相同的响应。

1. 尝试

所以我将配置中的位置行更改为:

location /api {

不幸的是,这不起作用,相反,我收到一个错误Cannot GET /api,这是一个节点错误,所以显然它被路由到 api,但有些东西仍然很臭。

2. 尝试

URI 中的尾部斜杠似乎是必需的,因此我将其添加到该位置:

location /api/ {

现在有些事情发生了变化。我不会得到与以前相同的错误,而是得到“301 永久移动”。如何修复我的 nginx 配置文件?

有关环境的附加信息

我正在使用 kubernetes 部署来部署 nginx 反向代理(包括)。引入的配置。然后我使用 kubernetes 服务公开 nginx。另外,我尝试使用 kubernetes ingress 来处理这种情况,使用相同的路由,但是,ingress 服务会响应一条default backend …

nginx node.js kubernetes

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

HTTP 拦截器未完成(即从未调用 Finalize)

在我的 Angular 8 项目中,我实现了最简单的方法HttpInterceptor,只传递请求,而不执行任何操作

\n\n

在我的 Angular 8 项目中,我实现了一个简单的方法HttpInterceptor,只需克隆原始请求并添加一个参数:

\n\n
@Injectable()\nexport class RequestHeadersInterceptor implements HttpInterceptor {\n\n  intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {\n    // original code return next.handle(request) // pass-by request as-is\n\n    return next.handle(request.clone({\n      params: request.params.set(\'language\', \'en\') }\n    ));\n  }\n}\n\n
Run Code Online (Sandbox Code Playgroud)\n\n

在我的服务中,我有一个getFoos()方法可以进行 HTTP 调用,该调用将被以下内容拦截RequestHeadersInterceptor

\n\n
import { HttpClient } from \'@angular/common/http\';\nimport { Injectable } from \'@angular/core\';\nimport { finalize } from \'rxjs/operators\';\nimport { Foo } from \'.\';\n\n@Injectable({\n  providedIn: \'root\'\n})\nexport class FooService {\n  constructor(private …
Run Code Online (Sandbox Code Playgroud)

rxjs typescript angular-http-interceptors angular

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

注入具有多个相同类型参数的构造函数

我使用autofac作为DI容器。我的目标是将参数store注入构造函数。那就是我的构造函数的样子。

public SomeClass (IMyCouchStore store)
{
    this.store = store;
} 
Run Code Online (Sandbox Code Playgroud)

store参数需要两个字符串参数才能实例化:

// sample instantiation
var store = new MyCouchStore("http://someUri","someDbName");
Run Code Online (Sandbox Code Playgroud)

我试图在引导过程中注册两个参数:

builder
    .RegisterType<MyCouchStore>()
    .As<IMyCouchStore>()
    .WithParameters(new [] {
        new NamedParameter("dbUri","http://someUri"),
        new NamedParameter("dbName","someDbName")
    }
Run Code Online (Sandbox Code Playgroud)

但是,我收到以下错误:

Autofac.Core.DependencyResolutionException

无法在类型为'MyCouch.MyCouchStore'的长度为2的多个构造函数之间进行选择。注册组件时,使用UsingConstructor()配置方法显式选择构造函数。

如何注入多个相同类型的参数?

c# autofac

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

ASP.NET MVC5以自定义格式显示日期

我正在尝试格式化DateTime变量,以便将其显示为MMMM dd, yyyy,例如,2011年6月10日.

在我看来,我有:

@Html.Raw(modelItem => item.MyDateTimeProperty.ToString("MMMM dd, yyyy"))
Run Code Online (Sandbox Code Playgroud)

这给我一个例外如下:

lambda expression cannot be converted to 'string' because 'string' is not
a delegate type
Run Code Online (Sandbox Code Playgroud)

当然,item.MyDateTimeProperty数据类型是DateTime.

作为参考,我使用了以下示例[1]

// The example displays the following output: 
//    Today is June 10, 2011. 
DateTime thisDate1 = new DateTime(2011, 6, 10);
Console.WriteLine("Today is " + thisDate1.ToString("MMMM dd, yyyy") + ".");
Run Code Online (Sandbox Code Playgroud)

我究竟做错了什么?

编辑

我看到stack247答案是理想环境中最合适的解决方案.但是,在我的情况下,控制器返回一个在我的模型类中不存在的对象.这是因为我正在使用API​​来获取数据.这就是为什么Anrei我的解决方案最适合我的原因.

@Html.Raw(item.MyDateTimeProperty.ToString("MMMM dd, yyyy"))
Run Code Online (Sandbox Code Playgroud)

[1] http://msdn.microsoft.com/en-US/library/8kb3ddd4%28v=vs.110%29.aspx

c# asp.net-mvc datetime tostring razor

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

LINQ:无法将类型“System.Int32”转换为“System.Object”

我是 MVC、EF 和 LINQ 的新手。

我首先使用 EF 和 DB 从 DB 创建所有模型。我还创建了一个 ViewModel“CourseDetailsVM”:

public class CourseDetailsVM
{
    public int CourseId { get; set; }
    public int CategoryFk { get; set; }
    public int SoftwareFk { get; set; }
    public int TeacherFk { get; set; }
    public string CourseCode { get; set; }
    public string CourseCodeMs { get; set; }
    public string CourseName { get; set; }
    public string CourseDescriptionShort { get; set; }
    public int CourseEventId { get; set; } …
Run Code Online (Sandbox Code Playgroud)

.net c# linq asp.net-mvc entity-framework

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

为什么将[2..3..10]解释为[2; 5; 8]

当我声明以下列表时

let list = [2..3..10]
Run Code Online (Sandbox Code Playgroud)

F#interactive将其解释为包含的列表[2; 5; 8].该控制台输出将是:

let list = [2..3..10]
;;

val list : int list = [2; 5; 8]
Run Code Online (Sandbox Code Playgroud)

背后的数学是什么?我没有看到输入如何与输出相关.

f# functional-programming

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