【问题标题】:Resolving "Unhandled rejection TypeError: jquery_1.default is not a function"解决“未处理的拒绝类型错误:jquery_1.default 不是函数”
【发布时间】:2017-07-15 16:22:44
【问题描述】:

我试图在我的 Aurelia 附加函数中调用使用 jQuery。每当 jQuery 命令运行时,我都会收到此错误:

未处理的拒绝类型错误:jquery_1.default 不是函数。

这是我的 app.ts 代码:

    import {Redirect, NavigationInstruction, RouterConfiguration} from 'aurelia-router';
    import 'semantic';
    import $ from 'jquery';

    export class App {
      configureRouter(config: RouterConfiguration): void {
        config.title = 'TrackRack';
        config.options.hashChange = false;
        config.options.root = '/';
        config.map([
          { route: ['home'], name: 'home', moduleId: 'views/home' },
          { route: '', name: 'home2', moduleId: 'views/home'}
        ]);
      }
      attached() {
          $('.nav_menu').visibility({
            type: 'fixed'
          });
      }
    }

这是我的配置:

            {
                "name": "jquery",
                "path": "../node_modules/jquery/dist",
                "main": "jquery.js"
                },
                {
                "name": "semantic",
                "path": "../node_modules/semantic-ui/dist",
                "main": "semantic.js",
                "resources": [
                    "semantic.min.css"
                ]
            }

【问题讨论】:

    标签: jquery typescript asp.net-core aurelia


    【解决方案1】:

    您需要在 aurelia.json 中做的就是

    "jquery"

    没有必要比 jQuery 更复杂。

    另外,请注意,您可以在视图中使用ref 自定义属性来获取对视图中元素的引用。您可以将此引用传递给 jQuery,而不是搜索元素。

    app.html

    <template>
      ...
      <nav ref="navMenu">
        ...
      </nav>
      ...
    </template>
    

    app.ts

     $(this.navMenu)).visibility({
         type: 'fixed'
     });
    

    【讨论】:

    • 除此之外,我必须将我的导入更改为这个才能正常运行 ==> import * as $ from 'jquery'
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-06-11
    • 1970-01-01
    • 2019-01-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-17
    相关资源
    最近更新 更多