0

I want to show users which I got in "data" variable

I got an warning on line which says "object access via string literals is disallowed (no-string-literal)"

this.crud = data['msg'];

My class :

export class CrudClass {
    public id?: string;
    public  name: string;
    public email: string;
    public password: string;
}

My Component :

export class ListComponent implements OnInit {
    private crud: CrudClass[];

    constructor(private router: Router, private apiRoutes: ApiRoutesService) { }

    ngOnInit() {
        this.readApp();
    }

    readApp() {
        this.apiRoutes.readApp().subscribe(
            data => {
                console.log(data);
                this.crud = data['msg'];
            },
            error => {
                console.log(error);
            }
        );
    }
}
1
  • I would create an interface in ApiRouteService that includes the value you need to access, and define the return type of readApp() (in service) to be an observable of that type. Then you can access it via dot notation. Check this sample: stackblitz.com/edit/angular-npvara Commented Oct 15, 2019 at 13:26

2 Answers 2

3

you can do it multiple ways

First Just disable the rule

/* tslint:disable:no-string-literal */
 this.crud = data['msg'];
/* tslint:enable:no-string-literal */

Second Use a variable instead of a string literal

change

this.crud = data['msg'];

to

let key='msg';
this.crud = data[key];

Or to disable it globally set no-string-literal: false in tslint.config.json

Related SO post

Sign up to request clarification or add additional context in comments.

2 Comments

You should rather disable the rule in the tslint.json file with no-string-literal: false, other than that I agree !
@MoHiTPaNdEy which one you tried can you show the updated code
0

You can try like setting a let object to a value and set it to the data property

Like

let dataKey='msg';
this.crud = data[dataKey]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.