简体   繁体   English

angular-cli选择了jquery

[英]angular-cli chosen jquery

part of angular-cli.json angular-cli.json的一部分

"styles": [
    "styles.css",
    "../node_modules/font-awesome/css/font-awesome.css",
    "../node_modules/bootstrap/dist/css/bootstrap.min.css",
    "../node_modules/bootstrap-daterangepicker/daterangepicker.css",
    "../node_modules/bootstrap-chosen/bootstrap-chosen.css"
],
"scripts": [
    "../node_modules/hammerjs/hammer.min.js",
    "../node_modules/jquery/dist/jquery.min.js",
    "../node_modules/bootstrap-chosen/dist/chosen.jquery-1.4.2/chosen.jquery.js"
  ],

blaBla.component.html blaBla.component.html

<div class="col-lg-3">
    <select appChosen multiple data-placeholder="Choose a Country" class="chosen chzn-done" tabindex="2">
        <option value=""></option>
        <option value="United States">United States</option>
        <option value="United Kingdom">United Kingdom</option>
        <option value="Afghanistan">Afghanistan</option>
    </select>
</div>

blaBla.directive.ts blaBla.directive.ts

import { Directive, ElementRef } from '@angular/core';
import * as $ from 'jquery';

@Directive({
    selector: '[appChosen]'
})
export class ChosenDirective {

    constructor(private el: ElementRef) {}
    ngAfterViewInit() {
        $(this.el.nativeElement).chosen({ allow_single_deselect: true });
    }
}

chosen doesn't work error in console: 选择在控制台中不起作用:

__WEBPACK_IMPORTED_MODULE_1_jquery__(...).chosen is not a function __WEBPACK_IMPORTED_MODULE_1_jquery __(...)。被选中不是一个函数

but $('.chosen').chosen(); 但是$('.chosen').chosen(); works in chrome dev tools. 适用于chrome dev工具。

How can this be fixed? 怎么解决这个问题?

i find solution here https://github.com/angular/angular-cli/issues/2760 我在这里找到解决方案https://github.com/angular/angular-cli/issues/2760

thanks to twmobius 感谢twmobius

so here it is: angular-cli.json 所以这里是:angular-cli.json

"scripts": [
    "../vendor.ts"
  ],

vendor.ts vendor.ts

import * as $ from 'jquery';
window['$'] = window['jQuery'] = $;
require('./node_modules/chosen-js/chosen.jquery.js');

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

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