Javascript 角度4无法读取属性';更换';未定义错误的定义

Javascript 角度4无法读取属性';更换';未定义错误的定义,javascript,angular,typescript,Javascript,Angular,Typescript,我在其中一个字符串上使用Angular 4中的.replace()JS函数来去除几个字符。在组件中,我编写了如下代码: @Component({...}) export class SomeComponent implements OnInit { routerUrl: string = ''; constructor() {} ngOnInit() { this.routerUrl = "SOME_DYNAMICALLY_RETRIEVED_STRING"; this.r

我在其中一个字符串上使用Angular 4中的
.replace()
JS函数来去除几个字符。在组件中,我编写了如下代码:

@Component({...})
export class SomeComponent implements OnInit {
 routerUrl: string = '';

 constructor() {}

 ngOnInit() {
   this.routerUrl = "SOME_DYNAMICALLY_RETRIEVED_STRING";
   this.routerUrl = this.routeUrl.replace(/[`~!@#$%^&*()_|+\-=?;:'",.<>\{\}\[\]\\\/]/gi, '');
   console.log(this.routerUrl);
 }
请帮我解决这个问题

添加代码

export class CourseComponent implements OnInit {
routeUrl: string = '';

  constructor(private renderer: Renderer2, private route: ActivatedRoute) {}

  ngOnInit() {
    this.route.params.subscribe((params: Params) => {
      this.routeUrl = params[':name'];
      this.routeUrl = this.routeUrl.replace(/[`~!@#$%^&*()_|+\-=?;:'",.<>\{\}\[\]\\\/]/gi, '');
      console.log(this.routeUrl);
    })
  }
}
导出类CourseComponent实现OnInit{
routeUrl:string='';
构造函数(私有呈现器:Renderer2,私有路由:ActivatedRoute){}
恩戈尼尼特(){
this.route.params.subscribe((params:params)=>{
this.routeUrl=params[':name'];
this.routeUrl=this.routeUrl.replace(/[`~!@$%^&*()([u124;+\-=?;:'”,.\{\\[\]\\/]/gi,);
console.log(this.routeUrl);
})
}
}

如果您要订阅某个URL,请按如下所示在订阅中放置替换

this.router.url.subscribe((routeparams){
   this.routerUrl = "SOME_DYNAMICALLY_RETRIEVED_STRING";
   if(this.routerUrl){
        this.routerUrl = 
         this.routeUrl.replace(/[`~!@#$%^&*()_|+\-=?;:'",.<>\{\}\[\]\\\/]/gi, '');

      }
})
this.router.url.subscribe((routeparams){
this.routerUrl=“一些动态检索的字符串”;
如果(此.routerUrl){
this.routerUrl=
this.routeUrl.replace(/[`~!@$%^&*()|+\-=?;:',.\{\\\[\]\/]/gi,);
}
})

replace
在原位不起作用(不会发生变异)。你必须分配它
this.routeUrl=this.routeUrl.replace(…)
鉴于SafeSubscriber上的
,我怀疑错误来自代码中的其他地方。_next
错误消息的一部分。或者,在执行代码时未设置动态检索的字符串。顺便问一下,这个regexp应该做什么?@Kinduser是的,但这不能解释他所犯的错误。@torazaburo是的,只是我的一点提示:)@torazaburo这个regex只是去掉了所有我不想要的字符,只保留了字母表、数字和几个字符。
this.router.url.subscribe((routeparams){
   this.routerUrl = "SOME_DYNAMICALLY_RETRIEVED_STRING";
   if(this.routerUrl){
        this.routerUrl = 
         this.routeUrl.replace(/[`~!@#$%^&*()_|+\-=?;:'",.<>\{\}\[\]\\\/]/gi, '');

      }
})