gpt4 book ai didi

angular - 功能模块不导出组件

转载 作者:太空狗 更新时间:2023-10-29 17:04:47 24 4
gpt4 key购买 nike

我的简单应用程序有一个名为 AppModule 的根模块和一个名为 HomeModule 的功能模块。我正在尝试为名为 HomeComponent 的组件(它是 HomeModule 的一部分)创建一个路由,但我得到的只是

Module "HomeModule" has no exported member 'HomeComponent'

应用程序路由.ts

import { Routes, RouterModule } from '@angular/router'
import { HomeComponent } from './Home/home.module.ts' // HomeComponent not found

const appRoutes: Routes = [
{ path: '', component: HomeComponent }
];

export const appRoutingProviders: any[] = [

];

export const routing = RouterModule.forRoot(appRoutes);

home.module.ts

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

import { HomeComponent } from './home.component.ts';

@NgModule({
imports: [ BrowserModule ],
declarations: [ HomeComponent ],
exports: [ HomeComponent ]
})

export class HomeModule { }

home.component.ts

import { Component } from '@angular/core';

@Component({
selector: 'home',
template: '<h1>Hello World</h1>'
})

export class HomeComponent { }

为什么 HomeModule 不导出其组件?

最佳答案

引用 1:http://angularjs.blogspot.com/2016/08/angular-2-rc5-ngmodules-lazy-loading.html引用 2:https://angular.io/docs/ts/latest/guide/ngmodule.html#!#imports

问题 1:NgModel 导入/导出

import { HomeComponent } from './Home/home.module.ts' // HomeComponent not found

home.module.ts 导出 HomeModule,而不是 HomeComponent

@NgModule 导出使那些导出组件的 Angular2 特性 可用于导入模块。 特性可以是模板指令、选择器、可注入(inject)服务等。

Home 组件 feature 是它的选择器。因此,将 HomeModule 导入 app.module 将使 HomeComponent 选择器 home 可用于任何 app.module 的组件。

要让 HomeModule 显式导出 HomeComponent,需要 index.js 和 index.d.ts。 (灵感来自 Fabio Antunes 的回答)

使用选择器home

要使用它,exports: [ HomeComponent ] 在 home.module.ts 中是必需的。

app.module.ts

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

import { AppComponent } from './app.component';

import { HomeModule } from './Home/home.module';

import { routing } from './app.routing';

@NgModule({
imports: [
BrowserModule,
HomeModule,
routing],
declarations: [
AppComponent],
bootstrap: [AppComponent]
})
export class AppModule { }

app.component.ts 使用选择器 home

import {Component} from '@angular/core';

@Component({
selector: 'app-component',
template: `
<h1>{{title}}</h1>
<home></home>`
})
export class AppComponent {
title = 'APP';
}

使用 HomeComponent

直接使用HomeComponent需要在./Home中加入index.js和index.d.ts

./Home/index.js

exports.HomeModule = require('./home.module').HomeModule;
exports.HomeComponent = require('./home.component').HomeComponent;

./Home/index.d.ts

exports * './home.module';
exports * from './home.component';

然后像npm包一样引入Home Module

app.routing.ts

// We are importing the module directory, not ./Home/home.module.ts
import { HomeComponent } from './Home';

import { Routes, RouterModule } from '@angular/router'

const appRoutes: Routes = [
{ path: '', component: HomeComponent }
];

export const appRoutingProviders: any[] = [

];

export const routing = RouterModule.forRoot(appRoutes);

问题 2:路由(到子模块)

要路由到模块,子模块需要有自己的路由设置。

在父路由(app.routing.ts)中使用loadChildren

在子路由 (home.routing.ts) 中使用 RouterModule.forChild

app.module.ts

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

import { AppComponent } from './app.component';

import { HomeModule } from './Home/home.module';

import { routing } from './app.routing';

@NgModule({
imports: [
BrowserModule,
HomeModule,
routing],
declarations: [
AppComponent],
bootstrap: [AppComponent]
})
export class AppModule { }

app.component.ts

import {Component} from '@angular/core';

@Component({
selector: 'app-component',
template: `
<h1>{{title}}</h1>
<nav>
<a routerLink="/home" routerLinkActive="active">Home</a>
</nav>
<router-outlet></router-outlet>`
})
export class AppComponent {
title = 'APP';
}

app.routing.ts

import { Routes, RouterModule } from '@angular/router';

const appRoutes: Routes = [
{
path: '',
redirectTo: '/home',
pathMatch: 'full'
},
{
path: 'home',
loadChildren: './Home/home.module'
}
];

export const appRoutingProviders: any[] = [

];

export const routing = RouterModule.forRoot(appRoutes);

主页/home.module.ts

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

import { HomeComponent } from './home.component';

import { routing } from './home.routing';

@NgModule({
imports: [
BrowserModule,
routing],
declarations: [
HomeComponent]
})

export class HomeModule { }

主页/home.routing.ts

import { Routes, RouterModule } from '@angular/router';

import { HomeComponent } from './home.component';

const homeRoutes: Routes = [
{
path: '',
redirectTo: '/home',
pathMatch: 'full'
},
{
path: 'home',
component: HomeComponent
}
];

export const appRoutingProviders: any[] = [

];

export const routing = RouterModule.forChild(homeRoutes);

关于angular - 功能模块不导出组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38881228/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com