简体   繁体   English

为什么我的 angular 服务变量未定义?

[英]Why my angular service variable is undefined?

i've been trying to get a varaible from one component to another in my code and yet it returns undefined我一直试图在我的代码中从一个组件到另一个组件获取一个变量,但它返回未定义

my service我的服务

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

@Injectable({
  providedIn: 'root'
})
export class ColorService {

  public color : string;

  constructor() { }
}

My app module我的应用模块

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

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

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

import { ColorService } from './core/color.service';

import { UserComponent } from './user/user.component';
import { PublicComponent } from './public/public.component';

const routes : Routes = [
  {
    path: '',
    redirectTo: 'user',
    pathMatch: 'full'
  },
  {
    path: 'user',
    component: UserComponent
  },
  {
    path: 'public',
    component: PublicComponent
  },
  {
    path: '**',
    redirectTo: 'user'
  }
]

@NgModule({
  declarations: [
    AppComponent,
    UserComponent,
    PublicComponent
  ],
  imports: [
    BrowserModule,
    RouterModule.forRoot(routes)
  ],
  providers: [
    ColorService
  ],
  bootstrap: [AppComponent]
})
export class AppModule { }

my user component我的用户组件

import { Component, OnInit } from '@angular/core';
import { ColorService } from '../core/color.service';

@Component({
  selector: 'app-user',
  templateUrl: './user.component.html',
  styleUrls: ['./user.component.scss']
})
export class UserComponent implements OnInit {

  constructor(private color: ColorService) { }

  ngOnInit(): void {

  }

  colorit(){
    this.color.color = 'blue';
    console.log(this.color.color);
  }

}

and the component that should get the color string以及应该获取颜色字符串的组件

import { Component, OnInit } from '@angular/core';
import { ColorService } from '../core/color.service';

@Component({
  selector: 'app-public',
  templateUrl: './public.component.html',
  styleUrls: ['./public.component.scss']
})
export class PublicComponent implements OnInit {

  constructor(private color: ColorService) { }

  ngOnInit(): void {

  }

  getColor(){
    console.log(this.color.color);
  }
}

yet, everytime that i try to get the color in the public component i get a undefined, even tought it's included within the providers of appModule然而,每次我尝试在公共组件中获取颜色时,我都会得到一个未定义的,甚至是包含在 appModule 的提供程序中的

For communicating from one component to another component, we can use @Input and @Output emitter properties.对于从一个组件到另一个组件的通信,我们可以使用@Input 和@Output 发射器属性。

you can update your code as below in PublicComponent.ts file:您可以在 PublicComponent.ts 文件中更新您的代码,如下所示:

import { Component, OnInit, Input } from '@angular/core';
import { ColorService } from '../core/color.service';

@Component({
  selector: 'app-public',
  templateUrl: './public.component.html',
  styleUrls: ['./public.component.scss']
})
export class PublicComponent implements OnInit {
  private _color = '';

  constructor(private color: ColorService) { }

  ngOnInit(): void {}

  @Input()
  set color(color: string) {
     this._color = color || '<no name set>';
  }

  getColor(){
    console.log(this.color.color);
  }
}

Also, add the following line of code in PublicComponent.html file:此外,在 PublicComponent.html 文件中添加以下代码行:

<app-public [color]="color.color"></app-public>

Please make the following changes and let me know if it works fine for you.请进行以下更改,并让我知道它是否适合您。

Inside your component you have method called colorinit where as that method was not declared in the ngoninit lifecycle.在您的组件内部,您有一个名为 colorinit 的方法,因为该方法未在 ngoninit 生命周期中声明。 So could you confirm whether that method your calling from the component html?那么您能否确认您是否从组件 html 调用该方法?

If not please add that method name inside the lifecycle hook如果不是,请在生命周期钩子中添加该方法名称

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM