簡體   English   中英

為什么我在組件中導入的 js 不起作用?

[英]why the js I import in my component dosen't work?

I have Jquery function in mt angular navbar, those function doesn't seems to work once I route link to another component but those function are important for my navbar to be used, so I put them in a js file that I added to my angular .json 腳本字段,因此即使我路由到另一個組件也可以加載它,但現在我不知道為什么當我嘗試使用包含我的 ZD223E1439188E4783249D5247EZ 代碼的 function 時它不起作用。

navabr.component.ts

import {Component, OnInit} from '@angular/core';
import {AuthService} from "../auth.service";
import {clientType} from "../clientType";
import {navbarutil} from './navbarUtilities.js';
declare var $;

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


  constructor() {
  }

  ngOnInit(): void {
    navbarutil.switchstate(); //launch with a collapsed navbar
    navbarutil.unwindOnCollapsed();
    navbarutil.buttonSwitch();
  }

 
}

navabrUtilities.js

function unwindOnCollapsed() {
  $(document).ready(function() {
    $('a.collapsed').click(() => {
      console.log('hey '+this.isOpen());
      if (!(this.isOpen())) {
        this.switchstate()
      }
    });
  });
}

function buttonSwitch() {
  $(document).ready(function() {
    $('#bouttonmenunavbar').click(() => {
      this.switchstate();
    });
  });
}

function switchstate() {
  $(document).ready(function() {
    console.log('yes');
    $('.sidebar').toggleClass('fliph');
    $('.innerlist').collapse('hide');
  });
}

function isOpen() {
  return $('.sidebar').hasClass('fliph');
}

angular.json

"scripts": [
           "./src/app/navbar/navbarUtilities.js"
            ]

你能幫我找到如何讓它們工作嗎?

function buttonSwitch() {
  $(document).ready(function() {
    $('#bouttonmenunavbar').click(() => {
      this.switchstate();
    });
  });
}

這個問題可能在這里this.switchState() this上下文是文檔的 function。 您必須編寫一個箭頭 function 才能獲得包含方法switchState的 class 的 this 。

function buttonSwitch() {
  $(document).ready(() => { // here is the modification
    $('#bouttonmenunavbar').click(() => {
      this.switchstate();
    });
  });
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM