Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/32.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 修饰符9中不支持函数表达式_Angular_Typescript_Arrow Functions - Fatal编程技术网

Angular 修饰符9中不支持函数表达式

Angular 修饰符9中不支持函数表达式,angular,typescript,arrow-functions,Angular,Typescript,Arrow Functions,我正在处理Angular 9,并在运行时生成动态路由值,为此我使用ComplexUrlRouter,如下所示,并在路由中使用相同的路由。 但它给了我以下的错误 错误:src/app/app.module.ts(33,45):在模板编译“批准”时出错 不支持表达式形式。 src/app/utility/complex.url.matcher.ts(4,10):在“ComplexUrlMatcher”的模板编译过程中出错 修饰符中不支持函数表达式 考虑将函数表达式转换为导出函数。 app.modul

我正在处理Angular 9,并在运行时生成动态路由值,为此我使用ComplexUrlRouter,如下所示,并在路由中使用相同的路由。 但它给了我以下的错误

错误:src/app/app.module.ts(33,45):在模板编译“批准”时出错 不支持表达式形式。 src/app/utility/complex.url.matcher.ts(4,10):在“ComplexUrlMatcher”的模板编译过程中出错 修饰符中不支持函数表达式 考虑将函数表达式转换为导出函数。

app.module.ts

export const AppRoutes: Routes = [
  { path: '',   component: HomeComponent, pathMatch: 'full' },
  {
    path: 'blog/:id',
    component: ArticleComponent,
    children: [
      {
        path: '',
        component: ArticleComponent
      },
      {
        matcher: ComplexUrlMatcher("title", /[a-zA-Z0-9]+/),
        component: ArticleComponent
      },
    ]
  },{
    path:'add-blog',component:AddPostComponent
  },
  {
    path:'category/:id',
    component:MenuDetailsComponent
  },
  { path: '**', redirectTo: '' }
];
complex.url.matcher.ts

import { UrlSegment, UrlSegmentGroup, Route } from '@angular/router';

export function ComplexUrlMatcher(paramName: string, regex: RegExp) {
  return (
    segments: UrlSegment[],
    segmentGroup: UrlSegmentGroup,
    route: Route) => {
    const parts = [regex];
    const posParams: { [key: string]: UrlSegment } = {};
    const consumed: UrlSegment[] = [];

    let currentIndex = 0;

    for (let i = 0; i < parts.length; ++i) {
      if (currentIndex >= segments.length) {
        return null;
      }
      const current = segments[currentIndex];

      const part = parts[i];
      if (!part.test(current.path)) {
        return null;
      }

      posParams[paramName] = current;
      consumed.push(current);
      currentIndex++;
    }

    if (route.pathMatch === 'full' &&
      (segmentGroup.hasChildren() || currentIndex < segments.length)) {
      return null;
    }

    return { consumed, posParams };
  }
}

从'@angular/router'导入{urlsection,urlsectiongroup,Route};
导出函数ComplexUrlMatcher(参数名称:字符串,正则表达式:正则表达式){
返回(
段:URLSEMENT[],
segmentGroup:UrlSegmentGroup,
路线:路线)=>{
常量部分=[regex];
常量posParams:{[key:string]:URLSEMENT}={};
消耗的常量:URLSEMENT[]=[];
设currentIndex=0;
对于(设i=0;i=segments.length){
返回null;
}
常数电流=段[currentIndex];
常数部分=部分[i];
如果(!部件测试(当前路径)){
返回null;
}
posParams[paramName]=当前;
消耗。推送(当前);
currentIndex++;
}
如果(route.pathMatch==='full'&&
(segmentGroup.hasChildren()| | currentIndex
我搜索了许多StackOverflow帖子,但无法解决相同的问题。
我检查了Angular Docs Url,其中提到“在版本5和更高版本中,编译器在发出.js文件时自动执行此重写。”我使用Angular 9,但仍然出现错误。

AOT内部装饰程序不允许内联函数调用

考虑将内联函数调用转换为函数引用:

export const AppRoutes: Routes = [
  { path: '',   component: HomeComponent, pathMatch: 'full' },
  {
    path: 'blog/:id',
    component: ArticleComponent,
    children: [
      {
        path: '',
        component: ArticleComponent
      },
      {
        matcher: complexUrlMatcher,
        component: ArticleComponent
      },
    ]
  },{
    path:'add-blog',component:AddPostComponent
  },
  {
    path:'category/:id',
    component:MenuDetailsComponent
  },
  { path: '**', redirectTo: '' }
];

export const complexUrlMatcher=complexUrlMatcher(“标题”/[a-zA-Z0-9]+/);
导出函数ComplexUrlMatcher(参数名称:字符串,正则表达式:正则表达式){
返回(
段:URLSEMENT[],
segmentGroup:UrlSegmentGroup,
路线:路线)=>{
常量部分=[regex];
常量posParams:{[key:string]:URLSEMENT}={};
消耗的常量:URLSEMENT[]=[];
设currentIndex=0;
对于(设i=0;i=segments.length){
返回null;
}
常数电流=段[currentIndex];
常数部分=部分[i];
如果(!部件测试(当前路径)){
返回null;
}
posParams[paramName]=当前;
消耗。推送(当前);
currentIndex++;
}
如果(route.pathMatch==='full'&&
(segmentGroup.hasChildren()| | currentIndex
如果src/app/utility/complex.url.matcher.ts(2,61)中出现以下错误,则此解决方案不起作用:不支持“complexUrlMatcher”表达式表单的模板编译期间出错。Src/App/Uptual/Field.URL .MatHel.TS(5,10):在“GoulurLMatter”函数表达式的模板编译过程中,错误不支持在装饰器中考虑将函数表达式转换为导出函数。
export const complexUrlMatcher = ComplexUrlMatcher("title", /[a-zA-Z0-9]+/);

export function ComplexUrlMatcher(paramName: string, regex: RegExp) {
  return (
    segments: UrlSegment[],
    segmentGroup: UrlSegmentGroup,
    route: Route) => {
    const parts = [regex];
    const posParams: { [key: string]: UrlSegment } = {};
    const consumed: UrlSegment[] = [];

    let currentIndex = 0;

    for (let i = 0; i < parts.length; ++i) {
      if (currentIndex >= segments.length) {
        return null;
      }
      const current = segments[currentIndex];

      const part = parts[i];
      if (!part.test(current.path)) {
        return null;
      }

      posParams[paramName] = current;
      consumed.push(current);
      currentIndex++;
    }

    if (route.pathMatch === 'full' &&
      (segmentGroup.hasChildren() || currentIndex < segments.length)) {
      return null;
    }

    return { consumed, posParams };
  }
}