user111111 user111111 - 7 months ago 76
TypeScript Question

Angular2 returning node instead of Element

export class AppComponent {
title = 'Dashboard';
navbarmenu = NAVBAR;

constructor(router:Router){ NavigationEvent) => {
if(document.querySelector('.a-active')) document.querySelector('.a-active').classList.remove('a-active');

setTimeout(() => {
let testTry = document.querySelector('.submenu li');

let anchor = testTry.parentNode.parentNode.parentNode.children[0];

// anchor is a node and throwing an error.


Error: Property 'classList' does not exist on type 'Node'

I can't start my server if this setTimeout function is not commented. But when the server is running and uncomment. It runs properly but still get an error.

Thanks. Any help would be appreciated.

setTimeout(() => {
    let isActive = document.querySelector('.submenu li')

    if(isActive) isActive.closest('ul').closest('li').children[0].classList.add('a-active')

Try this workaround! I am not sure why it returns node. In plain javascript, your code should work. Maybe it's a bug on typescript.

Polyfill: or

Notify me if it worked or not.