标签: angular6

router.navigate不起作用(Angular6,延迟加载)

我是Angular 4+的新手(目前正在使用v.6).我一直在尝试使用this.router.navigate(['/ landing'])函数从登录组件重定向到登陆组件,它无法正常工作.它将显示登录页面一秒钟,然后再次重定向回登录页面.

但是,如果我尝试浏览正常链接,例如,

<a routerLink="/landing">landing</a>
Run Code Online (Sandbox Code Playgroud)

它工作正常.

这是我的app-routing.module.ts

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { Routes, RouterModule } from '@angular/router';

const routes: Routes = [
  {
    path: 'login',
    loadChildren: './login/login.module#LoginModule'
  },
  {
    path: 'landing',
    loadChildren: './landing/landing.module#LandingModule'
  },
  {
    path: '',
    redirectTo: 'landing',
    pathMatch: 'full'
  },
  {
    path: '**',
    redirectTo: 'landing',
    pathMatch: 'full'
  }
];

@NgModule({
  imports: [ RouterModule.forRoot(routes) ],
  exports: [ RouterModule ]
})
export class AppRoutingModule { }
Run Code Online (Sandbox Code Playgroud)

这是登录组件中的代码,

angular angular6

7
推荐指数
1
解决办法
3549
查看次数

命名的出口组件加载到根组件中

我正在尝试使用辅助路由实现sidenav,但没有成功.

这是我的路由器的简化示例:

{
        path: 'admin',
        component: MainLayoutComponent,
        children: [
            {
                path: '',
                component: ReportsPageComponent
            },
            {
                path: ':userID',
                component: ReportsUserPageComponent,
                resolve: {userID: UserIDResolver},
                children: [
                    {
                        path: 'day-detail/:day',
                        outlet: 'sidenav',
                        component: DayDetailComponent,
                        resolve: {dayRange: DayRangeResolver},
                    },
                ]
            },
        ]
    }
Run Code Online (Sandbox Code Playgroud)

main-layout.component.html:

<div class="content">
    <router-outlet></router-outlet>
</div>
<router-outlet name="sidenav"></router-outlet>
Run Code Online (Sandbox Code Playgroud)

然后在ReportsUserPageComponent中导航

this.router.navigate(['./', { outlets: { sidenav: ['day-detail', new Date().toJSON()] } }], { relativeTo: this.route });
Run Code Online (Sandbox Code Playgroud)

这创造了这样的东西

admin/9/(sidenav:day-detail/2018-06-22T13:28:11.737Z)
Run Code Online (Sandbox Code Playgroud)

问题是它不想加载DayDetailComponent.我需要这个作为ReportsUserPageComponent的子项,因为我不想要这样的东西,admin/(9//sidenav:day-detail/2018-06-20T22:00:00.000Z)因为我回去后,那些括号保留在那里(admin/(9))没有明显的原因.

angular-routing angular angular-router angular6

7
推荐指数
0
解决办法
313
查看次数

Angular 6创建单例服务

我知道Angular 6中必须创建单例服务的方式已经发生了一些变化。我有一个身份验证服务,需要为访问它的每个组件构造一次。我将提供程序设置为服务的根目录:

@Injectable({
  providedIn: 'root'
})
Run Code Online (Sandbox Code Playgroud)

在我的app.module.ts文件中,我将AuthService设置为NgModule中的提供程序之一。但是,每当我在使用Auth服务的不同组件之间路由时,它都会创建Auth Service的新实例(从第一次调用它时清除数据)。如何确保仅将Auth Service实例化一次,然后在不同组件之间访问该实例?

authentication angularfire2 angular angular6

7
推荐指数
1
解决办法
6998
查看次数

angular6如何在另一个库中导入库?

正如标题所示,我使用了生成基础库ng generate library lib; 然后我生成了另一个名为的库lib2.我希望使用一些基本组件或服务lib,所以我lib在lib2模块中导入,但npm run build lib2失败了.

我收到了这个错误:error TS2307: Cannot find module 'lib'.

如何正确导入基础库?任何帮助表示赞赏.

我将测试代码放在https://github.com/dyh333/ng6-test中

angular-cli angular angular6 angular-cli-v6

7
推荐指数
1
解决办法
3719
查看次数

Angular 6 Scroll Listener重新加载组件

为了尽可能简单地解决这个问题,这是我的方案.

1)我有一个父组件,它有一个ViewContainerRef动态加载父组件中的组件.对于我的用例,parent是一个空的holder页面,它根据一组变量动态地替换另一个页面.

<ng-template #dynamic></ng-template>

2)子组件有一个水平滚动div,看起来类似于以下内容:

<div id="carousel" (scroll)="onScroll($event)"> <!-- List of components --> </div>

只要它与(scroll)侦听器一起滚动,它就会重新加载父组件,因此滚动位置总是会反弹回0,因为它已重新加载.

但是,如果我删除了(scroll)侦听器,则div会滚动而不会出现问题.问题是,我想根据水平滚动位置动态更改页面上的项目.

有没有人对嵌入式组件和滚动侦听器可能出现的问题有任何想法?非常感谢!

typescript angular angular6

7
推荐指数
1
解决办法
2533
查看次数

Angular6测试用例失败,无关错误消息

我已将Angular 4应用程序迁移到Angular 6,现在测试用例在终端中失败并显示以下错误消息,并且它在浏览器上显示成功.此外,有时测试用例通过而不显示任何错误消息.所以我们可以说我在运行测试时有60%的时间跟踪错误.

Chrome 67.0.3396 (Linux 0.0.0) ERROR
 {
    "message": {
      "isTrusted": true
    },
    "str": "[object ErrorEvent]"
  }
Run Code Online (Sandbox Code Playgroud)

我尝试升级和降级茉莉和茉莉核心

这是我的package.json

{
  "name": "anonymos",
  "version": "0.0.0",
  "scripts": {
    "ng": "ng",
    "start": "ng serve",
    "build": "ng build",
    "test": "ng test",
    "lint": "ng lint",
    "e2e": "ng e2e"
  },
  "private": true,
  "dependencies": {
    "@angular/animations": "^6.0.3",
    "@angular/common": "^6.0.3",
    "@angular/compiler": "^6.0.3",
    "@angular/core": "^6.0.3",
    "@angular/forms": "^6.0.3",
    "@angular/http": "^6.0.3",
    "@angular/platform-browser": "^6.0.3",
    "@angular/platform-browser-dynamic": "^6.0.3",
    "@angular/router": "^6.0.3",
    "@ng-bootstrap/ng-bootstrap": "^2.2.0",
    "angular-cookie": "^4.1.0",
    "angular-in-memory-web-api": "^0.6.0",
    "angular2-cookie": "^1.2.6",
    "angular2-recaptcha": "1.1.0",
    "body-parser": "1.18.3",
    "bootstrap": …
Run Code Online (Sandbox Code Playgroud)

karma-jasmine angular angular-test angular6

7
推荐指数
1
解决办法
681
查看次数

如何在角度6中使用jquery-ui

我已经尝试在StackOverflow上发布了多个方法,在角度6组件中使用jquery-ui,但它们都没有工作.例如,

  1. 我运行了npm install jquery jquery-ui来安装jquery和jquery-ui.

  2. 包含在angular.json中

    "scripts":["node_modules/jquery/dist/jquery.js","node_modules/jquery-ui-dist/jquery-ui.js",

错误如下:

AppComponent_Host.ngfactory.js [sm]:1ERROR TypeError: jquery__WEBPACK_IMPORTED_MODULE_1__(...).slider is not a function
    at AppComponent.push../src/app/app.component.ts.AppComponent.ngAfterContentInit (http:||localhost:4200/main.js:154:56)
    at callProviderLifecycles (http:||localhost:4200/vendor.js:42663:18)
    at callElementProvidersLifecycles (http:||localhost:4200/vendor.js:42644:13)
    at callLifecycleHooksChildrenFirst (http:||localhost:4200/vendor.js:42634:29)
    at checkAndUpdateView (http:||localhost:4200/vendor.js:43565:5)
    at callWithDebugContext (http:||localhost:4200/vendor.js:44454:25)
    at Object.debugCheckAndUpdateView [as checkAndUpdateView] (http:||localhost:4200/vendor.js:44132:12)
    at ViewRef_.push../node_modules/@angular/core/fesm5/core.js.ViewRef_.detectChanges (http:||localhost:4200/vendor.js:41948:22)
    at http:||localhost:4200/vendor.js:37684:63
    at Array.forEach (native)
Run Code Online (Sandbox Code Playgroud)

的index.html

<!DOCTYPE html>
<html lang="en">
<head>
<title>Car Dealer</title>
</head>
<body>
  <app-root></app-root>
</body> 
</html>
Run Code Online (Sandbox Code Playgroud)

app.component.html

<div id="slider">
</div>
Run Code Online (Sandbox Code Playgroud)

app.component.ts

import { Component, AfterContentInit } from '@angular/core';
import * …
Run Code Online (Sandbox Code Playgroud)

javascript jquery jquery-ui angular angular6

7
推荐指数
1
解决办法
8984
查看次数

Angular 6中的图像轮播

我需要一个图像轮播我的应用程序(Angular v6),而冲浪我发现这个解决方案我,使用ngx-drag-scroll.有没有其他方法可以像这样做图像轮播?

滚动

不使用jquery/javascript,只使用Typescript - 我能实现吗?

任何帮助全部资源.我想要一个旋转木马在其中显示卡组件.

carousel typescript angular angular6

7
推荐指数
1
解决办法
8843
查看次数

(角度6)路线转换动画不适用于子路线

我必须在路由器插座上设置路由器动画。实际上,我要动画的只是一个路由。这是我的路由模块:

export class CustomReuseStrategy extends RouteReuseStrategy {
...
...
  public shouldReuseRoute(future: ActivatedRouteSnapshot, curr: ActivatedRouteSnapshot): boolean {
    return  curr.component ? (<any>curr.component).name !== 'CardDetailComponent' : true;
  }
}

const wallRoutes: Routes = [
  {
    path: 'wall',
    component: WrapperComponent,
    children: [
      { path: '', component: CardListComponent },
      {
        path: 'placeslist/:idPlacesList/details/:id',
        component: CardDetailComponent,
        canActivate: [CardDetailsGuard],
        data: {
          state: 'details',
          reuse: false
        }
      },
      {
        path: 'placeslist/:id',
        component: PlaceslistDetailComponent,
        canActivate: [PlacesListDetailsGuard],
        data: {
          reuse: true
        }
      },
      {
        path: 'details/:id',
        component: CardDetailComponent,
        canActivate: [CardDetailsGuard],
        data: …
Run Code Online (Sandbox Code Playgroud)

angular2-routing angular-animations angular6

7
推荐指数
0
解决办法
400
查看次数

cdk虚拟滚动与粘性标题html表格角度

我从后端获取了超过 30000 条记录并在前端列出,因此使用 cdk-virtual-scroll 我可以实现这一目标。我创建了用 cdk-tag 括起来的普通表

          <cdk-virtual-scroll-viewport [itemSize] = "20">
            <div class="result_table">
             <table class="dataframe" border="1">
            <thead>
              <tr>
                <th >
                  Name
                </th>
                <th >
                  Description
                </th>
                <th >
                  Group
                </th>
                <th >
                  Data
                </th>
              </tr>
            </thead>
                <tbody>
                  <tr *cdkVirtualFor ="let data of address_obj_data_holder | filter:searchAddr">
                    <td >
                      {{data.name}}
                    </td>
                                             
                    <td >
                      {{data.description}}
                    </td>
                    <td >
                     {{data.group}}
                    </td> 
                    <td >
                     {{data.data}}
                    </td>
                  </tr>
                </tbody>
              </table>
            </div>
          </cdk-virtual-scroll-viewport>
Run Code Online (Sandbox Code Playgroud)

如果我这样做,当我向下滚动时,表格标题也会滚动,如果我喜欢,

          <cdk-virtual-scroll-viewport [itemSize] = "20">
            <div class="result_table">
             <table class="dataframe" border="1">
            <thead>
              <tr>
                <th style="width: …
Run Code Online (Sandbox Code Playgroud)

html css angular6 angular-cdk-virtual-scroll

7
推荐指数
1
解决办法
5950
查看次数