Angular 无法读取属性';loadChildren';尝试设置路径时未定义的
我需要有条件地加载路由路径。我在下面试过了。但它给出了这个错误。你能告诉我怎么做这种工作吗 [ng]无法读取未定义[ng]i的属性“loadChildren”时出错 wdm:未能编译 以及: 应用程序路由.module.ts:21未捕获类型错误:无法读取属性 未定义的“getLandingPage” at模块../src/app/app-routing.Module.ts(app-routing.Module.ts:21) 在网页上需要(引导:83) at模块../src/app/app.Module.ts(app.component.ts:21) 在网页上需要(引导:83) at模块../src/main.ts(main.ts:1) 在网页上需要(引导:83) 对象0处(main.ts:13) 在网页上需要(引导:83) 在CheckDeferredModule(引导:45) 在Array.webpackJsonpCallback[作为推送](引导:32) app.routing.module.ts 我有如下所示的Angular 无法读取属性';loadChildren';尝试设置路径时未定义的,angular,typescript,angular7,ionic4,Angular,Typescript,Angular7,Ionic4,我需要有条件地加载路由路径。我在下面试过了。但它给出了这个错误。你能告诉我怎么做这种工作吗 [ng]无法读取未定义[ng]i的属性“loadChildren”时出错 wdm:未能编译 以及: 应用程序路由.module.ts:21未捕获类型错误:无法读取属性 未定义的“getLandingPage” at模块../src/app/app-routing.Module.ts(app-routing.Module.ts:21) 在网页上需要(引导:83) at模块../src/app/app.Mod
auth.gurad.ts
。我不认为我在哪里可以用它来做这个
export class AuthGuard implements CanActivate {
constructor(private router: Router,
private user: UserService,
private localStorageService: LocalStorageService) { }
canActivate(next: ActivatedRouteSnapshot, state: RouterStateSnapshot): Promise<boolean> {
return new Promise(async resolve => {
const userInfo: UserInfo = await this.localStorageService.get(LocalStorage.USER_INFO);
if (userInfo && (moment() < moment(userInfo.expireDate))) {
this.user.guest = false;
return resolve(true);
}
this.user.guest = true;
this.router.navigate(["/sign-in"]);
return resolve(false);
});
}
}
导出类AuthGuard实现CanActivate{
构造函数(专用路由器:路由器、,
私有用户:UserService,
专用localStorageService:localStorageService){}
canActivate(下一个:ActivatedRouteSnapshot,状态:RouterStateSnashot):承诺{
返回新承诺(异步解析=>{
const userInfo:userInfo=wait this.localStorageService.get(LocalStorage.USER\u INFO);
if(userInfo&(moment()
如果您使用的是路由防护,为什么不在完全匹配中使用防护,并从那里重定向?在需要的地方使用条件句
例如:
const routes: Routes = [
{
path: "",
canActivate: [RouteGuard]
},
{
path: "tabs",
loadChildren: "./pages/tabs/tabs.module#TabsPageModule",
},
{
path: 'landing',
loadChildren: './pages/landing/landing.module#LandingPageModule'
},
];
RouteGuard:
canActivate(next: ActivatedRouteSnapshot, state: RouterStateSnapshot): Promise<boolean> {
return new Promise(async resolve => {
this.router.navigate([environment.hotelName]);
resolve(true)
}
}
canActivate(下一步:ActivatedRouteSnapshot,状态:RouterStateSnashot):承诺{
返回新承诺(异步解析=>{
这个.router.navigate([environment.hotelName]);
解析(真)
}
}
您应该在批准模块之外定义getLandingPage()
:
const getLandingPage = (): string => {
let url = "";
switch (hotelName) {
case "h1":
url = "tabs";
break;
case "h2":
url = "landing";
break;
default:
}
return url;
};
const routes: Routes = [
{
path: "",
redirectTo: getLandingPage(),
pathMatch: "full",
},
{
path: "tabs",
loadChildren: "./pages/tabs/tabs.module#TabsPageModule",
},
{
path: 'landing',
loadChildren: './pages/landing/landing.module#LandingPageModule'
},
];
Stackblitz示例()使用consthotelName
代替environment,因为Stackblitz没有定义环境变量
也不确定它与AOT配合得有多好。这与许多其他人使用胭脂逗号的经历类似。删除路由列表末尾的最后一个逗号,因为路由器认为有4个项目,最后一个是无效0
/未定义的项目,因此缺少加载子项代码>
const routes:routes=[
{
路径:“”,
重定向到:getLandingPage(),
路径匹配:“满”,
},
{
路径:“标签”,
loadChildren:“./页面/选项卡/选项卡.模块#选项卡模块”,
},
{
路径:'着陆',
loadChildren:“./pages/landing/landing.module#LandingPageModule”
}
];
(注意“landing”路线条目后缺少逗号)我今天遇到了相同的错误,无法读取未定义的属性“loadChildren”中的错误
我认为这个错误可能发生在很多方面,但对我来说,它只是一个CanActivate类上缺少的“导出”
我有:
@Injectable({providedIn: 'root'})
class MyActivator implements CanActivate
事实上,我应该:
@Injectable({providedIn: 'root'})
export class MyActivator implements CanActivate
这个错误有点棘手!所以我只想在这里提到它。检查路由器配置中的任何编程逻辑
当您在路由器配置中使用其他对象,然后使用具有字符串键和字符串值的简单对象时,可能会出现此问题
在我的例子中,在某个路由的数据对象内设置正则表达式时发生:
{
path: 'path',
component: MyComponent,
data: {
regEx: /^[a-f\d]{24}$/i,
},
},
编译器对此感到窒息,只是报告了非描述性错误:
无法读取未定义的属性“loadChildren”时出错
我在遇到此问题的人身上发现的其他示例也有关联。例如,有人使用常量作为解析程序的键:
const SOME_CONSTANT = 'someResolver',
{
path: 'path',
component: MyComponent,
resolve: {
[SOME_CONSTANT]: SomeResolver, // <-- you can't do this
},
},
const SOME_常量='someResolver',
{
路径:“路径”,
成分:MyComponent,
决心:{
[SOME_CONSTANT]:SomeResolver,//制表符/登录的定义在哪里?@jcuypers已更新。请查看。您需要在GetLandingPageURL中添加/前缀。您还可以复制并粘贴“/pages/”的定义吗?您能确认目标是延迟加载吗?@jcuypers是的,这是延迟加载的页面。
const SOME_CONSTANT = 'someResolver',
{
path: 'path',
component: MyComponent,
resolve: {
[SOME_CONSTANT]: SomeResolver, // <-- you can't do this
},
},