user111111 user111111 - 1 year ago 158
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.

Answer Source
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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download