值在Angular5中路由后丢失

值在Angular5中路由后丢失,angular,angular-routing,Angular,Angular Routing,我试图从forms.component.ts中的浏览器中获取一个值,并在student.component.ts中使用它,但在路由到student component后,我丢失了该值,我甚至尝试使用onInit和onDestroy保留该值,但无法这样做 我添加了一个dataservice.ts文件以将数据值存储为缓冲区,但浏览器中的值仍然没有显示,并且显示为未定义 请帮忙!! //forms.component.ts 从“@angular/core”导入{Component,OnInit,OnD

我试图从forms.component.ts中的浏览器中获取一个值,并在student.component.ts中使用它,但在路由到student component后,我丢失了该值,我甚至尝试使用onInit和onDestroy保留该值,但无法这样做

我添加了一个dataservice.ts文件以将数据值存储为缓冲区,但浏览器中的值仍然没有显示,并且显示为未定义 请帮忙!!
//forms.component.ts
从“@angular/core”导入{Component,OnInit,OnDestroy};
从'@angular/forms'导入{NgForm};
从“../DataService”导入{DataService};
@组成部分({
选择器:“应用程序窗体”,
templateUrl:'./form.component.html',
样式URL:['./form.component.css']
})
导出类FormComponent实现OnInit、OnDestroy{
name='kanav';
链接='';
res:字符串;
构造函数(公共数据服务:数据服务){
this.newVal=this.res;
}
str:字符串;
纽瓦尔:任何;
恩戈尼尼特(){
}
恩贡德斯特罗(){
this.dataservice.link=this.res;
this.dataservice.test=this.name;
}
sendValues():void{
this.res=this.str.substring(0,this.str.length-11)+'embedded=true';
console.log(this.res);
}

}
您需要使用@Injectable()注释您的服务,否则,在构造函数中定义服务时,它可能不会被实例化。或者,您在每个组件中都有不同的实例,如果您能告诉我如何在DataService类声明之前添加@Injectable(),并添加相应的导入,我们将不胜感激