如何在Angular 2+中将延迟加载的组件选择器包含到另一个组件的HTML中

吉里雅

我正在尝试在angular 2中实现延迟加载。
我已经通过以下链接创建了延迟加载
我有两个组件,例如home1和home2。home1是头条新闻,home2用于列出其他新闻。
第一次时,只会显示home1。
用户滚动页面后,必须将home2加载到home1中(就像在MVC中调用部分视图一样)。

我尝试通过在home1中调用home2作为 <app-home2-list></app-home2-list>

但出现错误。

错误

我不怎么在home1 html中调用home2 html ?.还有其他方法可以实现吗?

app.module.ts:

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


import { AppComponent } from './app.component';
import { MenuComponent } from './menu.component';
import { HomeComponent } from './home/home.component';
import { Const_Routing } from './app.routing';
import { HttpModule } from '@angular/http';
import { Home2ListComponent } from './home2/home2-list/home2-list.component';
import { Home1ListComponent } from './home1/home1-list/home1-list.component';



@NgModule({
  declarations: [
    AppComponent,
    MenuComponent,
    HomeComponent,
    Home1ListComponent,
    Home2ListComponent
  ],
  imports: [
    BrowserModule,
    Const_Routing,
    HttpModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

home1-list.component.ts和home2-list.component.ts(两个代码相同,api调用不同):

import { Component, OnInit, ViewChild, ElementRef } from '@angular/core';
import { ViewEncapsulation } from '@angular/compiler/src/core';
import { DatePipe } from '@angular/common';
import { Router } from '@angular/router';
import '../../../assets/scripts/endlessriver.js';
import * as $ from 'jquery';
import { SharedService } from '../../Services/shared.service';
import { Home1Service } from './home1.service';
import { Home2ListComponent } from '../../home2/home2-list/home2-list.component';

declare var jQuery: any;

@Component({
  selector: 'app-home1-list',
  templateUrl: './home1-list.component.html',
  styleUrls: ['./home1-list.component.css','../../../assets/styles/common.css','../../../assets/styles/endlessriver.css'],
  providers: [Home1Service,SharedService]
})
export class Home1ListComponent implements OnInit {

  constructor(public Service: Home1Service,public CommonService:SharedService) { }

  @ViewChild('marqueeID') input: ElementRef;

  HomeList:any;
  HomeList1:any;
  HomeList2:any;
  HomeList3:any;
  sectionName:string;
  datetime:string;
  productId:number=2;
  getListingData(sectionName)
  {
              this.Service.getListingNews(sectionName,15).subscribe(
                  data => {
                      this.HomeList = data.map(e => {
                          return { SectionName:e.ChildName,ArticleId:e.ArticleId, HeadLine: e.HeadLine, Abstract: e.Abstract, ImageLink: e.ImageLink ,UpdatedDate:this.CommonService.getDateFormat(new Date(e.UpdatedDate),'others').toString()};
                      })
                  },
                  error => { console.log(error) });
                  this.Service.getListingNews("world",5).subscribe(
                    data => {
                        this.HomeList1 = data.map(e => {
                            return { Heading:'world',SectionName:e.ChildName,ArticleId:e.ArticleId, HeadLine: e.HeadLine, Abstract: e.Abstract, ImageLink: e.ImageLink ,UpdatedDate:this.CommonService.getDateFormat(new Date(e.UpdatedDate),'others').toString()};
                        })
                    },
                    error => { console.log(error) });
                    this.Service.getListingNews("national",5).subscribe(
                        data => {
                            this.HomeList2 = data.map(e => {
                                return {Heading:'national', SectionName:e.ChildName,ArticleId:e.ArticleId, HeadLine: e.HeadLine, Abstract: e.Abstract, ImageLink: e.ImageLink ,UpdatedDate:this.CommonService.getDateFormat(new Date(e.UpdatedDate),'others').toString()};
                            })
                        },
                        error => { console.log(error) });
                        this.Service.getListingNews("state",5).subscribe(
                            data => {
                                this.HomeList3 = data.map(e => {
                                    return { Heading:'state',SectionName:e.ChildName,ArticleId:e.ArticleId, HeadLine: e.HeadLine, Abstract: e.Abstract, ImageLink: e.ImageLink ,UpdatedDate:this.CommonService.getDateFormat(new Date(e.UpdatedDate),'others').toString()};
                                })
                            },
                            error => { console.log(error) });
  }
  getHomeList(name: string) 
  {
    if(name=="0")
    {
      return this.HomeList1;
    }
    else if(name=="1")
    {
      return this.HomeList2;
    }
    else
    {
      return this.HomeList3;
    }
  }

  ngOnInit() {
      this.datetime=this.CommonService.getDateFormat(new Date(),'home').toString();
    this.getListingData('TopNews');
  }

  ngAfterViewInit() {

    jQuery(this.input.nativeElement).endlessRiver({

    });
    $( document ).ready(function() {
        $('.brkngBody').show();
    });
  }
}

home1.module.ts:

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';

import { Home1RoutingModule } from './home1-routing.module';
import { Home1ListComponent } from './home1-list/home1-list.component';
import { Home2ListComponent } from '../home2/home2-list/home2-list.component';
import { Home2RoutingModule } from '../home2/home2-routing.module';
import { Home2Module } from '../home2/home2.module';

@NgModule({
  imports: [
    CommonModule,
    Home1RoutingModule,
    Home2ListComponent
  ],
  exports:[
    Home2ListComponent
  ],
  declarations: [Home1ListComponent]
})
export class Home1Module { }

home2.module.ts:

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';

import { Home2RoutingModule } from './home2-routing.module';
import { Home2ListComponent } from './home2-list/home2-list.component';

@NgModule({
  imports: [
    CommonModule,
    Home2RoutingModule
  ],
  declarations: [Home2ListComponent]
})
export class Home2Module { }

home1-routing.module.ts:

import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { Home1ListComponent } from './home1-list/home1-list.component';

const routes: Routes = [ {
  path: '',
  component: Home1ListComponent
}];

@NgModule({
  imports: [RouterModule.forChild(routes)],
  exports: [RouterModule]
})
export class Home1RoutingModule { }

演示版

Santosh Singh

尝试以下代码段。

//home2.module.ts:

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';

import { Home2RoutingModule } from './home2-routing.module';
import { Home2ListComponent } from './home2-list/home2-list.component';

@NgModule({
  imports: [
    CommonModule,
    Home2RoutingModule
  ],
  exports:[Home2ListComponent],
  declarations: [Home2ListComponent]
})
export class Home2Module { }

//Home1.module.ts

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';

import { Home1RoutingModule } from './home1-routing.module';
import { Home1ListComponent } from './home1-list/home1-list.component';
import { Home2ListComponent } from '../home2/home2-list/home2-list.component';
import { Home2RoutingModule } from '../home2/home2-routing.module';
import { Home2Module } from '../home2/home2.module';

@NgModule({
  imports: [
    CommonModule,
    Home1RoutingModule,
    Home2ListComponent
  ],
  exports:[
    Home1ListComponent
  ],
  declarations: [Home1ListComponent]
})
export class Home1Module { }

演示

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在Angular 2中将组件导入另一个根组件

如何在angular2中将功能从一个组件触发到另一个组件?

如何在Angular 2中将对象从一个组件传递到另一个组件?

如何在Angular 2中从一个组件调用方法到另一个组件?

如何在angular2中调用另一个组件函数

如何在Angular2中的另一个组件中使用变量

如何在Angular2中将变量值从一个组件传递到另一个

如何在Angular 2中调用标头组件函数到另一个组件?

如何在 Angular 12 中将一个延迟加载模块组件访问到另一个延迟加载模块组件?

如何在Angular2的另一个组件中放置一个组件?

Angular2如何在另一个组件中使用一个组件功能

Swift 2:UIPinchGestureRecognizer中的选择器:如何从另一个类访问func

如何在此精确代码Angular 2及更高版本中显示另一个组件的值

如何从另一个组件调用一个组件中的方法是 angular2

如何动态地在另一个组件中显示一个组件 Angular2

使用选择器在Angular 2中动态加载组件

从另一个组件更改组件的属性,并在Angular 2的html中呈现它

如何在另一个Angle 2组件中设置变量

VueJs 2:如何在另一个组件中显示 prop?

Angular 2+:将组件用作另一个组件中的输入

如何在Angular中的另一个动态加载的组件内渲染组件?

Angular 2:在另一个组件上定义一个路由器,而不是引导的组件

我们可以将html模板从angular2中的一个组件传递到另一个组件吗?

如何在Angular 2-HTML中绑定另一个元素的宽度?

Angular 2,Cant能够从另一个组件访问一个组件中的变量

Angular 2 - 如何在导航到另一个组件时等待 observable 和 queryParams 时显示微调器?

登录重定向到另一个组件Angular 2后,重新加载导航组件

如何在Android中的另一个活动中访问一个活动中的组件(例如时间选择器)?

VueJs 2:如何在另一个组件中使用一个组件功能?